首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OnBindViewHolder on autoScroll to top

OnBindViewHolder on autoScroll to top
EN

Stack Overflow用户
提问于 2019-08-14 13:11:41
回答 2查看 1.4K关注 0票数 3

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

我尝试了每一个答案,所以关于自动投稿在更新列表在回收视图,但无法解决问题。

RecyclerView: Disable scrolling caused by change in focus

RecyclerView scrolls to top on notifyDataSetChanged in chat screen

android notifyItemRangeInserted disable autoscroll

这是我的适配器:

代码语言:javascript
复制
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都会被调用,并且在做任何事情之前,列表会像调用scrollToPosition一样滚动到0位置。

编辑:

代码语言:javascript
复制
private fun setView() {

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

布局:

代码语言:javascript
复制
      <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>

项目:

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-08-20 08:13:18

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

票数 2
EN

Stack Overflow用户

发布于 2019-08-14 14:07:03

用于比较DiffUtil提交的列表的Post重写方法。我也面临着类似的问题(我的问题是RecycleView将注意力集中在显示加载视图的list_item_network_state上)。“每隔几秒钟就会从服务器刷新列表,如果列表向下滚动,onBindViewHolder会将其滚动回0",所以当数据被更新并且包含相同的项目时,您想要关注旧列表中的焦点元素吗?

有点难找到解决你的问题(信息不够),我认为问题不是在你的适配器。

另外一个建议是使用ListAdapter,它本身就有DiffUtil.ItemCallback。

更新,所以如果onBIndViewHolder中没有任何内容,它仍然会滚动到顶部,这意味着问题并不存在。问题在于提交新列表(因为DiffUtil“它用来计算RecyclerView适配器的更新。”)。在比较过程中,它决定删除/更新/添加项的位置。这可能有一个问题(这是我对您提供的信息的假设,如果可能的话,向您的问题中添加更多代码)。检查方法为areItemsTheSame() (例如,它必须比较项的id ),并在调用areContentsTheSame()之后检查已更改的内容。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57495436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档