Sunday, 4 December 2016

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


EmoticonEmoticon