RecyclerView is a ViewGroup added to the android studio as a successor of the GridView and ListView. It is an improvement on both of them and can be found in the latest v-7 support packages. It has been created to make possible construction of any lists with XML layouts as an item which can be customized vastly while improving on the efficiency of ListViews and GridViews. This improvement is achieved by recycling the views which are out of the visibility of the user. For example, if a user scrolled down to a position where items 4 and 5 are visible; items 1, 2, and 3 would be cleared from the memory to reduce memory consumption.
Implementation: To implement a basic RecyclerView three sub-parts are needed to be constructed which offer the users the degree of control they require in making varying designs of their choice.
- The Card Layout: The card layout is an XML layout which will be treated as an item for the list created by the RecyclerView.
- The ViewHolder: The ViewHolder is a java class that stores the reference to the card layout views that have to be dynamically modified during the execution of the program by a list of data obtained either by online databases or added in some other way.
- The Data Class: The Data class is a custom java class that acts as a structure for holding the information for every item of the RecyclerView.
Below is the implementation of the RecyclerView:
exam_card.xml
<!-- XML Code illustrating card layout usage. --><?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_width="match_parent" android:layout_height="105dp"> <TextView android:layout_width="200dp" android:id="@+id/examName" android:textSize="16sp" android:layout_marginStart="20dp" android:text="First Exam" android:textColor="@color/black" android:layout_marginEnd="20dp" android:maxLines="1" android:layout_marginTop="15dp" android:layout_height="wrap_content"/> <ImageView android:id="@+id/examPic" android:layout_width="20dp" android:layout_height="20dp" android:layout_below="@+id/examName" android:tint="#808080" android:layout_marginStart="20dp" android:layout_marginTop="7dp" app:srcCompat="@drawable/baseline_schedule_black_36dp"/> <TextView android:id="@+id/examDate" android:layout_toEndOf="@+id/examPic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/examName" android:layout_marginTop="5dp" android:layout_marginEnd="20dp" android:layout_marginStart="10dp" android:gravity="center" android:text="May 23, 2015" android:textSize="16sp"/> <ImageView android:id="@+id/examPic2" android:layout_width="20dp" android:layout_height="20dp" android:layout_below="@+id/examDate" android:tint="#808080" android:layout_marginStart="20dp" android:layout_marginTop="7dp" app:srcCompat="@drawable/baseline_school_black_36dp"/> <TextView android:id="@+id/examMessage" android:layout_toEndOf="@+id/examPic2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/examDate" android:layout_marginEnd="20dp" android:layout_marginTop="5dp" android:layout_marginStart="10dp" android:gravity="center" android:text="Best Of Luck" android:textSize="16sp"/> <TextView android:id="@+id/border2" android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginStart="15dp" android:layout_marginEnd="15dp" android:layout_alignParentBottom="true" android:background="#808080"/> </RelativeLayout> |
examViewHolder.java
// ViewHolder code for RecyclerView package com.example.admin.example; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.TextView; public class examViewHolder extends RecyclerView.ViewHolder { TextView examName; TextView examMessage; TextView examDate; View view; examViewHolder(View itemView) { super(itemView); examName = (TextView)itemView .findViewById(R.id.examName); examDate = (TextView)itemView .findViewById(R.id.examDate); examMessage = (TextView)itemView .findViewById(R.id.examMessage); view = itemView } } |

