Android Recylerview Tutorial.

Using RecyclerView You can design the following layout.



  1. Linear Vertical /Horizontal list.

  2. GridView.

  3. Staggered Grid where each view size is different.imagess


Why RecyclerView?


As the name suggest it reuses the view. It is good to use when the data is large.Suppose if there are 1000 contacts  User will scroll up or down to see more items,if currently visible item is out of view, this item’s view can be recycled and reused whenever a new item comes into view. you can refer the below screenshot to get a clear idea.linear11.png

Each list item i.e each row is a View object in the above image and each View object may have ImageView , TextView  or any thing.

What is needed to implement a RecyclerView.



  1. RecyclerView

  2. LayoutManager:-Decides how to draw a view in recyclerview using Gridlayout,LinearLayout or Staggered Grid.

  3. Adapter:-What is to be display at which position. Basically it is a middleware between Recyclerview and datasource.

  4. DataSource:-database/arraylist/Json etc.


Steps to Use RecyclerView



  1. Add RecyclerView dependency to the Module build.gradle file.

  2. Add RecyclerView to your xml and initialize it in your java code with the help of findViewById().

  3. Specify which type of Layout you need with  the help of LayoutManager in Activity or Fragment java code.

  4. Create a row xml that would define the single row of your list.

  5. Create an Adapter which will set the data for each row. So adapter needs a data source.

  6. The Adapter in Recyclerview has following methods.

    1. onCreateViewHolder:-It is used to link row xml to RecyclerView.

    2. onBindViewHolder:-It is used to set the data in each position of the View.



  7. Now in Activity or fragment just set your adapter to Recyclerview.


Please refer the below video for complete implementation.

[youtube https://www.youtube.com/watch?v=RZEzs_3ISFg&w=560&h=315]

For the handling click events on RecyclerView Please Click Here.

IF YOU HAVE ANY SUGGESTIONS OR ANY ERRORS WHILE IMPLEMENTING , FEEL FREE TO COMMENT BELOW.


Happy Coding....

Comments

  1. […] Before watching this Do refer the previous Blog for implementing a RecyclerView. Please Click Here. […]

    ReplyDelete
  2. hello mem, how to Multiple(one layout in multiple textview) row layouts using RecyclerView.

    ReplyDelete

Post a Comment