gpt4 book ai didi

android - OnBindViewHolder 更新自动滚动到顶部

转载 作者:行者123 更新时间:2023-11-29 18:28:44 32 4
gpt4 key购买 nike

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

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

RecyclerView: Disable scrolling caused by change in focus

RecyclerView scrolls to top on notifyDataSetChanged in chat screen

android notifyItemRangeInserted disable autoscroll

等等

这是我的适配器:

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>

最佳答案

问题出在我的 DiffUtil 上。我鲁莽又草率,在 areItemsTheSameareContentsTheSame 上有错误/基本上相同的值,所以 diffUtil 看不出它们之间的区别,这意味着每个新内容都与新内容相同项目。

关于android - OnBindViewHolder 更新自动滚动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57495436/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com