Recycler View In Android

RecyclerView is the successor of Listview and Gridview in Android. The RecyclerView is available from the v7 support library. This is an improved version of Listview and GridView classes, provided by the Android framework. It has default animations, to add and remove views. It will recycle the view directly. Here, I am going to explain about how we can create a simple Recycler View, so that you can understand the methods available in this and the adapter. 

First, create your layout file, as shown below:
  1. <RelativeLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  4.  android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  
  5.  android:paddingRight="@dimen/activity_horizontal_margin"  
  6.  android:paddingTop="@dimen/activity_vertical_margin"  
  7.  android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">  
  8.     <android.support.v7.widget.RecyclerView  
  9.  android:id="@+id/my_recycler_view"  
  10.  android:layout_width="match_parent"  
  11.  android:layout_height="match_parent"  
  12.  android:scrollbars="vertical" />  
  13. </RelativeLayout>  
While creating a Recycler view, we need to set up the layout for this separately and we need to set the adapter also.

Now, we need to bind these UI elements into our main activity.
  1. RecyclerView view = (RecyclerView) findViewById(R.id.my_recycler_view);  
  2. LinearLayoutManager manager = new LinearLayoutManager(this);  
  3. view.setLayoutManager(manager);  
Now, we need to create a layout for the each row. Here, I have two textviews and one imageview. Please see the XML file, shown below:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.  android:layout_width="fill_parent"  
  3.  android:layout_height="?android:attr/listPreferredItemHeight"  
  4.  android:padding="6dip" >  
  5.   
  6.     <ImageView  
  7.  android:id="@+id/icon"  
  8.  android:layout_width="wrap_content"  
  9.  android:layout_height="fill_parent"  
  10.  android:layout_alignParentBottom="true"  
  11.  android:layout_alignParentTop="true"  
  12.  android:layout_marginRight="6dip"  
  13.  android:contentDescription="TODO"  
  14.  android:src="@drawable/abc_list_focused_holo" />  
  15.   
  16.     <TextView  
  17.  android:id="@+id/secondLine"  
  18.  android:layout_width="fill_parent"  
  19.  android:layout_height="26dip"  
  20.  android:layout_alignParentBottom="true"  
  21.  android:layout_alignParentRight="true"  
  22.  android:layout_toRightOf="@id/icon"  
  23.  android:ellipsize="marquee"  
  24.  android:singleLine="true"  
  25.  android:text="Description"  
  26.  android:textSize="12sp" />  
  27.   
  28.     <TextView  
  29.  android:id="@+id/firstLine"  
  30.  android:layout_width="fill_parent"  
  31.  android:layout_height="wrap_content"  
  32.  android:layout_above="@id/secondLine"  
  33.  android:layout_alignParentRight="true"  
  34.  android:layout_alignParentTop="true"  
  35.  android:layout_alignWithParentIfMissing="true"  
  36.  android:layout_toRightOf="@id/icon"  
  37.  android:gravity="center_vertical"  
  38.  android:text="Example application"  
  39.  android:textSize="16sp" />  
  40.   
  41. </RelativeLayout>   
Next is the main step, that is creating the Adapter for the recycler. For this, we need to create an inner class for the ViewHolder also.
 
Create a class named MyAdapter, that extends from RecyclerView.Adapter, shown below:
  1. class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>  
We need to implement the methods. There are three methods, which should be there in the RecyclerView Adapter. Please see them , shown below:
  1. onCreateViewHolder()  
Here, we need to inflate the layout and this will return the view holder for us.
  1. @Override  
  2. public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)  
  3. {  
  4.   
  5.     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);  
  6.     ViewHolder viewHolder = new ViewHolder(view);  
  7.     return viewHolder;  
  8. }  
onBindViewHolder()

Here, we can set the values for each item.
  1. @Override  
  2. public void onBindViewHolder(MyAdapter.ViewHolder holder, int position)  
  3. {  
  4.   
  5.     final String text = listItem.get(position);  
  6.     holder.titleTextView.setText(text);  
  7.     holder.summaryTextView.setText("Summary " + text);  
  8.   
getItemCount()

This will return the number of rows.
  1. @Override  
  2. public int getItemCount()  
  3. {  
  4.     return listItem.size();  
  5. }  
Now, we need to create a viewHolder inside the adapter class like the following,
  1. public class ViewHolder extends RecyclerView.ViewHolder  
  2. {  
  3.   
  4.     TextView titleTextView = null;  
  5.     TextView summaryTextView = null;  
  6.   
  7.     public ViewHolder(View itemView)  
  8.     {  
  9.         super(itemView);  
  10.         titleTextView = (TextView) itemView.findViewById(R.id.firstLine);  
  11.         summaryTextView = (TextView) itemView.findViewById(R.id.secondLine);  
  12.     }  
Now, set the adapter to the recycler view from the main activity.
  1. MyAdapter myAdapter = new MyAdapter(list);  
  2. view.setAdapter(myAdapter);  
Please see the screen shot below:
 


Similar Articles