首页 > 解决方案 > OnBindViewHolder on update autoScroll to top

问题描述

在适配器 submitList 到 DiffUtil 之后,调用 onBindViewHolder 并且由于某种原因它总是将列表滚动到顶部。

我尝试了 SO 中关于在 recyclerview 中更新列表时自动滚动的每一个答案,但无法解决问题。

IE

RecyclerView:禁用焦点变化引起的滚动

RecyclerView 在聊天屏幕中的 notifyDataSetChanged 上滚动到顶部

android notifyItemRangeInserted 禁用自动滚动

ETC

这是我的适配器:

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())
}

因此,在每次新提交列表后,无论当前位置是什么,都会调用 onBindViewHolder,在它执行任何操作之前,列表会滚动到 0 位置,就像调用 scrollToPosition 一样。

编辑:

private fun setView() {

  currentListAdapter = CurrentListAdapter()
  with(currenciesRv) {
        layoutManager = LinearLayoutManager(context)
        adapter = currentListAdapter
 }

布局:

      <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>

物品:

<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>

标签: androidkotlinandroid-recyclerviewandroid-architecture-componentsandroid-diffutils

解决方案


问题出在我的 DiffUtil 上。我鲁莽和草率,并且两者都有错误/基本相同的值areItemsTheSameareContentsTheSame因此 diffUtil 没有看到它们之间的区别,这意味着每个新内容都与新项目相同。


推荐阅读