Showing posts with label recyclerview. Show all posts
Showing posts with label recyclerview. Show all posts

Tuesday, October 3, 2017

Implementing Load More When Woking On Blogger's API

In this tutorial, i will share a tutorial about how to implementing load more and pagination function in Blogger API using retrofit.

When you working with API Blogger, you will face an endpoint that contains much data in list.

The URL that contains a lot of data in list is :
https://www.googleapis.com/blogger/v3/blogs/your-id-blog/posts?key=YOUR_AUTH_ID
That URL will display output json look like the following image :

Output List Json

If you run the URL above, the result will give you only 10 datas in first page. Take a look at attribute items.

To get the next page you have to request it again (load more) and display the result as pagination. To implementing the pagination (request the next page), you can use the attribute nextPageToken. As long as the attribute nextPageToken exist, it means the data still exist in the next page.

Request the first page


Just request the first data in normal URL (without params nextPageToken). In this case i request the data using retrofit :

On the code above, I request the first page and set the data into the adapter. Don't forget to save the value of nextPageToken.

In load more function, i check the nextPageToken is exist or not. Is exist, then request the next page.

Request the next page


To request the next page, you have to add params pageToken, and the value set from nextPageToken.

The URL to be :
https://www.googleapis.com/blogger/v3/blogs/your-id-blog/posts?key=YOUR_AUTH_ID&pageToken=CgkIChjigf_aoisQu_ugxZqtye83
The code is look like the follows :


That's all what i did to implementing load more pagination when request data using Blogger API. Hope it helps. Thank you.

Friday, January 6, 2017

Android Populating Different View/Layout On RecyclerView's Item

Sometime on your recyclerview's item, you need to place different view/layout on each item. For example on item index 0 you need to place a form login and on item index 1 you need to place a form register, etc.


Okay, that's the imagine of how to place a different layout on recyclerview's item. Now we start the coding.

PREPARATION

Compile the following library on your build.gradle (app:module)

compile 'com.android.support:recyclerview-v7:25.0.0'

IMPLEMENTATION

This project place 3 different item on recyclerview's item. So first we create all the layout before start java code

1. activity_main.xml

2. view_input_usernamepassword.xml 

3. view_please_wait.xml

4. view_detail_usernamepassword.xml

After the layouts are finish, now we create all the java class that we used on this project. Start from :
 
1. ItemModel.java

In order program know that item index 0 for Layout A, item index 1 for Layout B and etc, you have to give an indicator in your Object/Mode/Data before transfer to your adapter.

In this project I put an index inside my model class as an indicator. The model namely as ItemModel.java. Paste the following code :

2. ViewAdapter.java

Like i explained above (on the first paragraph), you have to create you inner class in ViewHolder (Whatever how much it is). If you wanna put 4 different layout, so create 4 different inner class of ViewHolder.

After that, you should to extends RecyclerView.Adapter<RecyclerView.ViewHolder> and then implement all of its methods.

Which method that explain that index 1 for layout A and etc?

For that, you have to override method getItemViewType(int position) manually. Create the condition using if and else. On this method you will use the indicator of your model as the key.

@Override
public int getItemViewType(int position) {
    ItemModel item = mListItemModel.get(position);
    if(item.getIndex()==0){
        return 0;
    }else if(item.getIndex()==1){
        return 1;
    }else if(item.getIndex()==2){
        return 2;
    }
    return super.getItemViewType(position);
}

These index or number got from model that we input on MainActivity (explained later).

After you create the condition, in onCreateViewHolder(ViewGroup parent, int viewType) you have to create the condition using case (or anything that related!). The logic is, if method getItemViewType() return index x, than put Layout x. Take a look at the code follows :

To get that the layout x has been return on method onCreateViewHolder(ViewGroup parent, int viewType), you must use an instanceof on method onBindViewHolder(RecyclerView.ViewHolder holder, final int position) . Take a look at the following sample code :

I hope you'll understand how to steps to create a complex adapter like above. Here is the full code of ViewAdapter.java :

3. MainActivity.java

In this class, as usual, just to set the data, define the recyclerview and set the adapter's data to recyclerview. Here the full code of MainActivity.java :

That's all. Compile and run the project. Hope it works well. Thank you.

Wednesday, January 4, 2017

Easy Tutorial Android Expandable RecyclerView

Today i going to share a awesome tutorial using recyclerview. As usual, you may use ListView for creating a collapse and expand view, right?

We using listview with reason that listview have a view called ExpandableListView, so we just set the data through ExapandableListView.


But in several case, your application required a Recyclerview (I don't know what it is) instead of Listview.

Indeed, create a expandable view using recyclerview is more difficult that using ExpandableListview. You have to create all materials or view not just set the data.

But don't worry, i have spent several hours just to create simple expandable view using recyclerview, with hope you will easy to understand the steps.

PREPARATION


Compile the following libraries :

compile 'com.android.support:design:23.2.0'
compile 'com.android.support:cardview-v7:23.2.0'
compile 'com.android.support:recyclerview-v7:23.2.0'

After that, download LIB MODULE EXPANDABLE , extract and then place in your main package. Confuse? Take a look at Figure 1 :

Figure 1

Place the LIB MODULE EXPANDABLE look like figure 1.

IMPLEMENTATION


Create new 3 xml layouts :
  1. activity_main.xml
  2. item_list_parent.xml
  3. item_list_child.xml

At Figure 1, there are several packages that have some java classes.

Package models
  1. BiodataChildModel.java
  2. BiodataParentModel.java

Package expandables
  1. BiodataChildViewHolder.java
  2. BiodataParentViewHolder.java
  3. BiodataExpandable.java
And last, definitely MainActivity.java.

First, modify the layouts that we've prepared.

activity_main.xml. Paste the following code :

item_list_child.xml. Paste the following code :

item_list_parent.xml. Paste the following code :

Second, we modify the code of all the java classes inside the package models. Why this package is first? Because this classes will use in next next java classes.

1. Package MODELS

Open BiodataChildModel.java. This class store all the data that will show on expand view. Paste the following code :

Then modify class BiodataParentModel.java. This class contains data from class BiodataChildModel and set as a list.

Because this class as the parent, you have to extends ParentListItem. After you do extends, you have to implements its methods. Take a look at the following code  :

Attention :
Make sure you have returned mListChild in method getChildItemList()

2. Package EXPANDABLES

Open class BiodataChildViewHolder.java. Paste the following code :

Open class BiodataParentViewHolder.java. Paste the following code :

In code above there are method :
  1. setExpanded(boolean expanded) 
  2. onExpansionToggled(boolean expanded) . 
Those method used to create an animation of the imageview (icon + and -) when the view is expand or collapse. You can see on the video demo above.

Open class BiodataExpandable.java. Paste the following code :

All the class that used to create expandable has done. Time to input the data and set adapter in your MainActivity.java. Paste the following code :

Done!!! Run your project, hope the project is running well :D. Thank you.

Download the project here

Sunday, December 4, 2016

Android How to Implement OnClickListener On RecyclerView

Recyclerview and Listview in two things that have several differences. One of these difference is How to implement onClickListener in both of them.


In Listview, we just do something like this :

It means, we just do something like code above on class that declare the listview. It's a little bit simple than Recyclerview.

How about on Recyclerview?

Actually there are many ways to implement onClickListener in RecyclerView. If you ask at Stackoverflow, they will give many answers to do this.

From this article i will give you one of many way how to implement onClickListener in RecyclerView.

See also : how to put different view/layout on recyclerview's item.

I do this on Adapter class. For simply, give an ID on RelativeLayout or LinearLayour in your Adapter's layout.

After that, define the LinearLayour or RelativeLayout in your Holder. Next, do the OnClick in onBindViewHolder method.

For simply, is using this project for example,  inside OnBindViewHolder method just do like below :


Done! Hope it helps, thank you.

Tutorial Swipe For Delete, Edit, Share Item on Android RecyclerView

Today i going to share you a tutorial using Ryclerview. This tutorial will show how to take an action by swapping the item on Recyclerview.
I using THIS LIBRARY for this tutorial. Take a look at the following steps.

PREPARATION


Compile this library to your build.gradle (Module :app) :

Prepare 2 layouts and give name like below :

  1. activity_main.xml
  2. swipe_layout.xml
Prepare 4 java class and give like below :
  1. MainActivity.java
  2. StudentModel.java
  3. DividetItemDecoration.java
  4. SwipeRecyclerViewAdapter.java

IMPLEMENTATION


1. Prepare all XML file that we'll use in this project

Create a divider layout in your Drawable folder, give name as divider.xml. Paste the code below :

Divider.xml used as the border line of recyclerview's item.


Modify your color.xml  or you can use the default color when the project created

Modify your style.xml, paste the code below :

Modify your activity_main.xml, paste the code below :

Modify your swipe_layout.xml, paste the code below :

2. Prepare all the Java class 

Modify your StudentModel.java like below :

After that, Modify your DividerItemDecoration.java until look like the following code :

Next, write down the following code in your SwipeRecyclerViewAdapter.java. Here the code :

Last, modify your MainActivity.java until similar like the following code :

That's all the steps for swiping on RecyclerView's item. Wait for other Tutorial.

BELOW IS THE COMPLETE PROJECT LINK : LINK GITHUB

Working With RecyclerView in Android Studio

This is my first article that using English. Actually this site based on Indonesia Language. For some reason i also using English in several article.

Today i gonna show you how to working with Recyclerview. A simple tutorial in order to you are easy to understand.

PREPARATION 

In this tutorial you just need to create three java classes, there are :

  1. MainActivity.java
  2. UserModel.java
  3. UserAdapter.java
And two xml layouts, there are :
  1. activity_main.xml
  2. item_user.xml

IMPLEMENTATION

First, compile the Recyclerview library on your build.gradle(Module:app), like the following :

Modify your activity_main.xml, like the following xml :

Next, create one more layout for the item, in this project named as item_user.xml :

Now, it's time to modify our java class, first we need to create a model for our user. In this project named UserModel.java

Second, we modify UserAdapter.java, this adapter have to extend RecyclerView.Adapter<>. But before do that, the first thing you should do is write the inner class and give name as ViewHolder and this inner class extends RecyclerView.ViewHolder. So the UserAdapter.java look like below :

Next step, you have to extend the UserAdapter.java to RecyclerView.Adapter<UserAdapter.ViewHolder>. ViewHolder comes from inner class that view have created before.

Next, implement all method that required to be implemented. After you implement all method, the Adapter look like this :

That all, the first thing we do, to setup our RecyclerView Adapter. Now just add some attribute that relevant with our required.

Full code of UserAdapter.java :

After you finish with your Adapter, now time to modify your MainActivity.java. Do Like code below :

The output will show like below :

Recyclerview output

Implementing Swipe For Delete Item And Undo On RecyclerView Android

After previous post, i was posted swipe for delete, edit and share item on Recyclerview. But on that post there's no Undo function to cancel delete item.

For more detail article please kindly read : Swipe for delete, edit and share item on Recyclerview


In this tutorial, i still use THIS PROJECT, so i just post the code that related with swipe function.

There is no much steps you will do, just write a several line codes in your MainActivity.java or wherever you define your Recyclerview. In this case, I define the Recyler view in MainActivity.java

For Undo function, we will use on the the newest materials called Snackbar. Snackbar will appears at the bottom when you delete the item.

For use Snackbar, make sure you have compiled this library in your build.gradle(Module:app) :

compile 'com.android.support:design:25.0.1

After that, in you class where you define the recyclerview, paste the code below :

That's all, enjoy it!!