Unexpected UI Freezes When Using RecyclerView with DiffUtil for Large Data Set on Android 14
I'm optimizing some code but I'm working on a project and hit a roadblock... I'm experiencing important UI freezes when updating a `RecyclerView` with a large dataset using `DiffUtil` in my Android app. This scenario occurs specifically on Android 14 devices. I've implemented `DiffUtil.Callback` to calculate item changes, but when I call `submitList()` on my `ListAdapter`, the UI becomes unresponsive for a noticeable amount of time, especially when the list contains over 1,000 items. Here's a simplified version of my adapter and `DiffUtil` implementation: ```kotlin class MyAdapter : ListAdapter<MyItem, MyViewHolder>(MyDiffCallback()) { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false) return MyViewHolder(view) } override fun onBindViewHolder(holder: MyViewHolder, position: Int) { val item = getItem(position) holder.bind(item) } } class MyDiffCallback : DiffUtil.ItemCallback<MyItem>() { override fun areItemsTheSame(oldItem: MyItem, newItem: MyItem): Boolean { return oldItem.id == newItem.id } override fun areContentsTheSame(oldItem: MyItem, newItem: MyItem): Boolean { return oldItem == newItem } } ``` I've tried running the `submitList()` call with `ListAdapter.submitList(newList, Runnable { /* UI thread work */ })` but the freezes continue. Additionally, I also tried simplifying the item layouts and ensuring that the bindings are not too heavy, but the question remains. The UI freezes for about 1-2 seconds during the `submitList()` call, which is not user-friendly. The logs show no unusual activity during this time, leading me to suspect it's an scenario with how the RecyclerView handles the updates internally. Is there a more efficient approach I can take to handle large datasets with `DiffUtil`, or is there a possibility that the scenario lies elsewhere in my implementation? Any insights would be greatly appreciated! I'm working on a web app that needs to handle this. I'm open to any suggestions.