Android RecyclerView List ExampleThe RecyclerView class extends the ViewGroup class and implements ScrollingView interface. It is introduced in Marshmallow. It is an advanced version of the ListView with improved performance and other benefits. RecyclerView is mostly used to design the user interface with the fine-grain control over the lists and grids of android application. In this tutorial, we will create a list of items with ImageView (for the icon) and TextView (for description) using RecyclerView and performs click listener on the item of its list. Android RecyclerView with List ExampleCreate an Android project, and add the RecyclerView support library com.android.support:recyclerview-v7:23.1.0 or above this version in build.gradle file. In the activity_main.xml file in layout directory, add the RecyclerView widget. activity_main.xmlCreate a dimens.xml file in values directory, and add the following code. dimens.xmlCreate a custom layout list_item.xml file with following code. list_item.xmlCreate a border.xml file in the drawable directory which is used to decorate the border of RecyclerView items. border.xmlCreate a MyListData.java class with the following code. This class is used as (POJO) class which sets the properties of the items. MyListData.javaCreate a MyListAdapter.java class and add the following code. This class extends RecyclerView.Adapter class and override its unimplemented methods. The onCreateViewHolder() methods inflates the list_item.xml. In the onBindViewHolder() method each data items are set to each row. MyListAdapter.javaFinally, in the MainActivity.java class, add the following code. This class creates the array of items for MyListData class and set the adapter class to RecyclerView. MainActivity.javaOutput:
Next TopicSwipe Del RecyclerView
|