Make sure to also enable ViewBinding in the app level build. Implementation ':glide:4.12.0' annotationProcessor ':compiler:4.12.0' // ViewModels and LiveDataĭef lifecycle_version = "2.3.1" implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version" kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" Implementation '2:retrofit:2.9.0' implementation '2:converter-gson:2.9.0' // Glide for Image Loading Retrofit and Gson Converter for Networking GsonConverter will play the role of converting JSON strings to Java objects. Retrofit will help us make network calls to the API. In this step, we will create a new project, you can refer to the image below for guidance.Īfter creating the project, we will add some dependencies to our app-level build Gradle. Then display them in a RecyclerView and add a click listener to handle clicks on each row. RecyclerView ItemDecorations This project showcases some ItemDecorations for RecyclerViews. While the second checks if the data between the two objects is the same.ĭiffUtil makes it possible for ListAdapter to change the items in the list. The first method checks if the two objects are the same (for example, based on ids). We override two of its methods areItemsTheSame(oldItem: Pojo, newItem: Pojo) and areContentsTheSame(oldItem: Pojo, newItem: Pojo). This class removes the need to call the notifyDataSetChanged() method.
What is a DiffUtil?Īccording to the official documentation: DiffUtil is a utility class that calculates the difference between two lists and outputs a list of update operations that converts the first list into the second one. Instantiate the ItemTouchHelper with the SimpleCallback callback and depending on which functionality you support, you should override onMove (RecyclerView, ViewHolder, ViewHolder) and / or onSwiped (ViewHolder, int) and and finally attach to your RecyclerView. It handles the addition and removal of items without the need to redraw the entire view. Just use the ItemTouchHelper class included in the RecyclerView support library. You can use submitList(POJO) when new data is available. While using a LiveData, a ListAdapter provides an easy way to provide data to the adapter. What is a ListAdapter?Īccording to the official documentation: A ListAdapter is a RecyclerView.Adapter base class for presenting List data in a RecyclerView, including computing diffs between Lists on a background thread. If it is your first time creating a RecyclerView you can take a look at this article. The RecyclerView class supports the display of a collection of data. The ability to handle clicks in a RecyclerView.Ī RecyclerView is a great widget to display a list of data items.Have some understanding of Jetpack libraries like ViewModel and LiveData.Īt the end of this tutorial, the reader should have:.
Know what Retrofit is and how to use it.Have worked with the basic RecyclerView to display data from an API (We will be fetching data from a REST API).Have some good understanding of Kotlin.