Build List Using RecyclerView In Android


 

Android RecyclerView with List Example –

First Create an Android Project with empty activity and open activiity_main.xml file in layout folder. After that add the RecyclerView widget.

activity_main.xml

Now we have to create a custom layout for listing items, So create list_layout.xml and add following code.

list_layout.xml

After creating list layout we need to create ListdataModel.java class. This class is used to sets the properties of the items.

ListdataModel.java

So now create ListAdapter.java class. This class extends RecyclerView.Adapter class and its methods like onCreateViewHolder() and onBindViewHolder(). onCreateViewHolder method inflates your list_layout.xml and the onBindViewHolder() method set each data items in list_layout.xml, also if you want to add a click event on any item then also you can do that in this method.

ListAdapter.Java

Now it’s time to write some code in MainActivty.java class. In this class, we create an array of items for Listdatamodel class and set the adapter class to RecyclerView.

MainActivity.java




















0 Comments