I try to submit the same list using AsyncListDiffer, but the recycler view always auto scroll to top. When I debug I see the recyclerview auto scroll to top after calling submitList before areItemsTheSame method is called
class TestPlaceAdapter(
private val itemClickCallback: (TestPlaceUi) -> Unit
) : RecyclerView.Adapter<TestPlaceAdapter.ItemViewHolder>() {
private val differ = AsyncListDiffer(this, DiffCallback())
private val listTest = mutableListOf<TestPlaceUi>()
fun setData(listPlace: List<TestPlaceUi>) {
if (listTest.size == 0) {
listTest.addAll(listPlace)
differ.submitList(listTest)
} else {
val test = mutableListOf<TestPlaceUi>().apply {
addAll(listTest)
}
differ.submitList(test)
}
}
private class DiffCallback : DiffUtil.ItemCallback<TestPlaceUi>() {
override fun areItemsTheSame(oldItem: TestPlaceUi, newItem: TestPlaceUi): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: TestPlaceUi, newItem: TestPlaceUi): Boolean {
return oldItem == newItem
}
}
...
}
TestPlaceUi is the data class and doesn't override equal and hashcode.
I detected my mistake caused by using another third party using skeleton. It reset adapter to recyclerview.