After the adapter submitList to the DiffUtil, onBindViewHolder is called and for some reason it always scroll the list to the top.
I tried every single one of the answers in SO regarding autoscroll on updating list in the recyclerview but can't solve the issue.
i.e.
RecyclerView: Disable scrolling caused by change in focus
RecyclerView scrolls to top on notifyDataSetChanged in chat screen
android notifyItemRangeInserted disable autoscroll
etc
Here is my adapter:
init {
setHasStableIds(true)
}
private val currentDiffer = AsyncListDiffer<Item>(this, MyDiffUtil())
...
override fun setHasStableIds(hasStableIds: Boolean) {
super.setHasStableIds(true)
}
override fun getItemCount(): Int {
return currentDiffer.currentList.size
}
override fun getItemId(position: Int): Long = currentDiffer.currentList[position].name.hashCode().toLong()
override fun getItemViewType(position: Int) = position
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
}
override fun onBindViewHolder(holder: CurrentListViewHolder, position: Int) {
holder.bindView(position, activity, currentDiffer.currentList[position], onCurrencyListener)
}
override fun onViewRecycled(holder: CurrentListViewHolder) {
holder.unbindView()
super.onViewRecycled(holder)
}
fun setData(list: LinkedList<Item>) {
val newList: LinkedList<Item> = LinkedList()
newList.addAll(list)
currentDiffer.submitList(newList.toList())
}
So after every new submit of the list, no matter what is the current position, onBindViewHolder is called and before it does anything the list is scrolled to 0 position like the scrollToPosition is called.
EDIT:
private fun setView() {
currentListAdapter = CurrentListAdapter()
with(currenciesRv) {
layoutManager = LinearLayoutManager(context)
adapter = currentListAdapter
}
layout:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/listRv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
item:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/itemLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/itemIvFlag"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="30dp"
android:layout_marginStart="15dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/itemIvFlag"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/itemTvISO"
android:layout_marginStart="10dp"
android:textStyle="bold"
tools:text="@string/dummy_number"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/itemTvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp"
android:textSize="14sp"
tools:text="@string/dummy_number"/>
</LinearLayout>
<EditText
android:id="@+id/itemEtAmount"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="15dp"
tools:text="@string/dummy_number"
android:inputType="numberDecimal"
android:enabled="false"
android:textColor="@drawable/et_disabled_color"
android:imeOptions="actionDone"
android:maxLines="1"
android:singleLine="true"/>
</RelativeLayout>
The problem was with my DiffUtil. I was reckless and sloppy and had wrong/basically the same values on both areItemsTheSame
and areContentsTheSame
so diffUtil saw no difference between them meaning that every new content was the same as the new item.