A day with Android Recycler View

1 min read

Recyclerview was introduced with Android 5.0 aka Android L. In simplest terms recycler view widget is advanced form of Android List View widget. Recycler view is Simpler but yet very powerful and enhances performance of android application when compared android list view.

A point to be noted here is that for application running on android devices having OS prior to Android L , Android Support library is required to be added .

Lets get our hands dirty with code :).

Changes in Main XML Layout
Changes in java Code use of Fragment.
Creating a customized row layout
Creating an adapter

Changes in Main XML Layout (main_layout.xml)

2 Changes in Java Code With Fragment
Although we are using Fragment for this User Interface but Android Activity can also be used to create UX as per need and recycler view can be added.

In onCreateView callback for your fragment inflate the xml layout we created in step 1, and initialize your recylcerviewList variable . Oh sorry before initialization of recylcerviewList variable its declaration is required.

RecyclerView recyclerViewList;

Now initialize your recyclerviewList. (MainFragment.java)

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.main_layout, null);
recyclerViewList = (RecyclerView)v.findViewById(R.id.recList);
return v;


In onActivityCreated callback for your fragment and create and intialize a variable LinearLayoutManager llm.

LinearLayoutManager llm = new LinearLayoutManager(getActivity());

set this as layout manager for recyclerview list. This step is important , otherwise if this step is missed even after successful implementation of other steps , nothing would be displayed on screen. Just to add my experience when first day I encountered recylcer view i did this mistake and wasted 4 hours to find out the problem. Ok. ok.. I know you people are intelligent and won’t do this silly mistake.

3. Creating a customized row layout (rowlayout.xml)