RecyclerView Android
In previous two blogs we have studied about how to create ListView with array adapter and Custom Adapter using BaseAdapter class. In this blog we are going to study about what is RecyclerView
and how to implement it in android. Before studying about it lets understand what is main difference between ListView and RecyclerView.
- RecyclerView is flexible version of ListView , If you want to show large data set in list or data that frequently change you should use RecyclerView.
- The views in RecyclerView are represented by View Holder objects. Each view holder represents a single view.
- RecyclerView creates view holder as are needed to display the on screen portion of the dynamic content.
- In RecyclerView the creating view and binding values with UI components are separating in thwo methods called : onCreateViewholder() and onBindViewHolder(). In other hand the ListView does not separating it, rather it create view and bind values with UI components in getView() method only.
- The LayoutManager can define layout of row views. In RecyclerView the LayoutManager class gives the option to define how to scroll the list, for eg. Vertically or horizontally then we can set LinearLayoutManager. For Grid we can set GridLayoutManager. In ListView does not having this kind of feature. ListView only able to vertically scrolling.
I hope you get the difference between ListView and RecyclerView. Now lets go for understanding to how to implement RecyclerView in Android.
To access the RecyclerView widget then you need to add V7 support libraries.
- Open app level gradle file and add following dependencies file.
- Now Sync your project
- Now you can add RecyclerView in your project.
Open your activity layout file and copy the following code and paste it in your layout file.
Now add the following code in your activity class of above xml file:
To feed all your data, you need to create adapter class, you must extend RecyclerView.Adapter class. The following code snippet shows an example of adapter class that consist of an two dataset : image arrays and string array displayed in ImageView and TextView respectively.
Now create one more layout file (here : "recycler_view_layout.xml") to design row view. See the following code:
Final step, Run your program and see the output. If any doubts and queries please comment in comment section.
Enjoy coding!😊
Click Here To Download Full Code
Comments
Post a Comment