In this blog, we are going to implement the DiffUtil with the RecyclerView
on android.
Today in each and every android application the data presentation to its user is the main role. Most of the time we use to present the data on the list so that users can easily view that data. Generally for displaying the list data we use ListView
or RecyclerView
.
Most of the case RecyclerView is used to display the list on android application. RecyclerView is an efficient and improved version of ListView
. It will be used for rendering a large amount of data and views.
List contains a large amount of information so for an efficient application the list uses should be smooth. With a large amount of information managing the list is a difficult job. If we update any of the entities on the data then with the RecyclerView
we have to reload the entire data and layouts as the new instance of the RecyclerView
adapter is created every time.
So for overcoming this limitation is used for this purpose. DiffUtil is a utility class that helps to compare the two lists and returns the updated list. Diffutils allows us to update the list without reloading or creating a new instance of the RecyclerView adapter
.
Implementation
RecyclerView
implementation:
RecyclerView
is mostly used to display the list items on the android applications so here for displaying the list with RecyclerView
add it on your activity XML file like below:
1 2 3 4 5 6 7 8 |
<androidx.recyclerview.widget.RecyclerView android:id="@+id/notification_list_rv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="@dimen/bottom_sheet_bottom_margin" tools:listitem="@layout/item_notification_list" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" tools:targetApi="lollipop" /> |
For adding the data on RecyclerView
create its adapter like below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class NotificationListRvAdapter(private val mContext: Context) : RecyclerView.Adapter<NotificationListRvAdapter.ViewHolder>() { private val mListData = ArrayList<NotificationList>() override fun onCreateViewHolder(p0: ViewGroup, p1: Int): NotificationListRvAdapter.ViewHolder { val view = LayoutInflater.from(mContext).inflate(R.layout.item_notification_list, p0, false) return ViewHolder(view) } override fun onBindViewHolder(holder: NotificationListRvAdapter.ViewHolder, position: Int) { val eachListData = mListData[position] holder.mBinding?.data = eachListData holder.mBinding?.handler = NotificationActivityHandler() holder.mBinding?.executePendingBindings() } override fun getItemCount(): Int { return mListData.size } fun setNotificationData(newListData: ArrayList<NotificationList>) { mListData.clear() mListData.addAll(newListData) } class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val mBinding: ItemNotificationListBinding? = DataBindingUtil.bind(itemView) } } |
So far we have created the list of the data using RecyclerView.
If we change any of the data entities for this applied data list and call inside our activity class. The entire created view will be recreated again.
Now needs to set the adapter and apply data to the RecyclerView
inside your activity like below:
1 2 3 4 5 |
private fun setNotificationListAdapter(notificationList: ArrayList<NotificationList>) { val notificationListRvAdapter = NotificationListRvAdapter(requireContext()) mContentViewBinding.notificationListRv.adapter = notificationListRvAdapter notificationListRvAdapter.setNotificationData(notificationList) } |
Using DiffUtil
DiffUtil is used to avoid the recreation of the entire created view of RecyclerView it will check the new and older datalist and set the newly updated list as a result.
DiffUtil.The callback is used to pass the older and updated data like below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class NotificationListRvAdapterDiffCallback(private val oldListData: ArrayList<NotificationList>, private val newListData: ArrayList<NotificationList>) : DiffUtil.Callback() { override fun getOldListSize(): Int = oldListData.size override fun getNewListSize(): Int = newListData.size override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { return oldListData[oldItemPosition].id === newListData.get(newItemPosition).id } override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { return oldListData[oldItemPosition] == newListData[newItemPosition] } @Nullable override fun getChangePayload(oldPosition: Int, newPosition: Int): Any? { return super.getChangePayload(oldPosition, newPosition) } } |
Now for using diffUtil we have to update the way of setting the adapter data for RecyclerView
like below :
1 2 3 4 5 6 7 8 |
fun setNotificationData(newListData: ArrayList<NotificationList>) { val notificationListRvAdapterDiffCallback = NotificationListRvAdapterDiffCallback(mListData, newListData) val diffResult = DiffUtil.calculateDiff(notificationListRvAdapterDiffCallback) mListData.clear() mListData.addAll(newListData) diffResult.dispatchUpdatesTo(this) } |
Conclusion:
In this blog, you have learned about the implementation of the DiffUtil on android for enhancing the RecyclerView
.
For more information regarding the DiffUtil follow the link
Thanks for reading this blog. You can also check other blogs from here.