【问题标题】:RecyclerView scroll returning to the top of the list when add new items [duplicate]添加新项目时RecyclerView滚动返回列表顶部[重复]
【发布时间】:2019-10-21 06:44:13
【问题描述】:

我正在开发一个应用程序,当用户点击滚动底部时,RecyclerView 会加载新数据。但是,列表已经完成并且具有固定大小,我只想将内容放入 Recycler,但是当我将内容插入 Recycler 时,滚动返回到列表顶部。然后我需要再次向下滚动,直到找到插入到列表底部的新数据。我想要的只是在加载新数据时保持滚动位置。

我尝试过使用 descendantFocusability = "blocksDescendants",但这不起作用。

这是我的 XML 代码,可以在其中找到 RecyclerView

<?xmlversion="1.0"encoding="utf-8"?>

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:headerLayout="@layout/top_navigation_bar"
tools:context=".MainActivity"
android:descendantFocusability="blocksDescendants">

<RelativeLayout
android:id="@+id/productsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:orientation="vertical"
android:background="#EBE7E7"
>

<TextView
android:id="@+id/productsLayoutTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:lineSpacingExtra="0sp"
android:text="@string/produtos_em_destaque"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="normal"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/productsLayoutTitle"
/>

<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignBottom="@+id/recyclerView"
android:scaleX="1"
android:scaleY="1"
android:progressTint="#1C36FF"
/>

</RelativeLayout>
</RelativeLayout>

用于向 RecyclersView 添加数据的代码是

fun showData(products:Product,productsListSize:Int){

if(!isStarted){

  recyclerView.apply{
   layoutManager=LinearLayoutManager(this@MainActivity)
   adapter=ViewHolderAdapter(products,productsListSize)
   (adapter as ViewHolderAdapter).notifyItemRangeInserted(0,productsListSize)
   pageNumber++
  }

  progressBar.visibility=View.GONE
  isStarted=true
}else{

  recyclerView.apply{
    adapter=ViewHolderAdapter(products,pageNumber*limitPerPage)
    (adapter as ViewHolderAdapter).notifyItemRangeInserted(((pageNumber-1)*limitPerPage),pageNumber*limitPerPage)
    pageNumber++
  }
  progressBar.visibility=View.GONE
}

}

我的 ViewHolderAdapter


packagecom.example.kotlinbasics

importandroid.graphics.Color
importandroid.view.LayoutInflater
importandroid.view.View
importandroid.view.ViewGroup
importandroid.widget.ImageView
importandroid.widget.RatingBar
importandroid.widget.TextView
importandroidx.recyclerview.widget.RecyclerView
importcom.squareup.picasso.Picasso
importkotlinx.android.synthetic.main.recyclerview_layout.view.*

class ViewHolderAdapter(private val products:Product,private val productsListSize:Int):RecyclerView.Adapter<ViewHolderAdapter.ViewHolder>(){

override fun onCreateViewHolder(parent:ViewGroup,viewType:Int):ViewHolder{
val view=LayoutInflater.from(parent.context).inflate(R.layout.recyclerview_layout,parent,false)
return ViewHolder(view)
}

override fun getItemCount()=productsListSize

override fun onBindViewHolder(holder:ViewHolder,position:Int){

holder.productName.text=products.produtos[position].nome
Picasso.get().load(products.produtos[position].img).into(holder.productImage)
}

class ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){

val productName:TextView=itemView.ProductName
val productImage:ImageView=itemView.ProductImage
}

}


所有应用程序都运行良好,唯一的问题是滚动行为。 当我在他中添加新数据时,我预计滚动位置不会到达 RecyclersView 列表的顶部。

【问题讨论】:

  • 这是数据变化时更换适配器造成的。您只应该更新它引用的列表并通知更改。
  • 你在新建一个adapter,每次都设置到RecyclerView,难怪会跳到顶部。

标签: android kotlin android-recyclerview


【解决方案1】:

当您每次都创建一个新适配器时,RecyclerView 将始终回到起始位置。您必须更新当前适配器,而不是使用整个列表创建另一个适配器。

您的适配器上应该有一个方法来管理项目列表,例如

fun updateList(product: Product) {
    myList.add(product.list)
}

在你的 else 分支中,你需要更新列表

recyclerView.apply{
    (adapter as? ViewHolderAdapter)?.updateList(products)
}

【讨论】:

  • 嗨 Diego,首先,谢谢你帮助我,你就是那个人。我是 Kotlin 的初学者,所以你能解释得更详细一点吗?我将不胜感激。
  • 我无法提供更多代码,因为我没有您的 ViewHolderAdapter 代码。但要点是您应该保持相同的适配器并且只更新数据。您可能会发现 this answer 有助于实现这一目标。
  • 我编辑并添加了 ViewHolderAdapter。他很简单
  • 我认为我的情况与您推荐我的链接有点不同,因为我在 ViewHolderAdapter 中不使用列表
  • 是的,这就是问题所在。您将需要在您的ViewHolderAdapter 中列出一个列表。适配器将接收附加元素以附加到其列表中,以允许 RecyclerView 保持当前位置。
【解决方案2】:

只需使用下面的代码,然后输入 requared 位置(在您的情况下 - 最后位置) recyclerView.scrollToPosition()

【讨论】:

    【解决方案3】:

    创建你的回收器视图适配器的一个实例,将原始列表传递给它。每当您想添加任何数据时,将其添加到列表中,然后通知适配器。 在您的情况下,只需添加产品列表并通知它

    private list<product> list = newArrayList<>();
    
    adapter= new ViewHolderAdapter(list);
    

    要添加数据时按钮的OnClick,只需将其插入列表中

    list.addAll(productlist);
    And now notify it to the adapter 
    adapter.notifyDataSetChange();
    

    你的情况

    fun showData(products:Product,productsListSize:Int){
    
    if(!isStarted){
    
             list.addAll(product)
              adapter.notifyDataSetChange();
              pageNumber++
        }
    
             progressBar.visibility=View.GONE
             isStarted=true
        }
        progressBar.visibility=View.GONE
     }
    
         }
    

    【讨论】:

    • 我理解你的意思,但就我而言,列表已经完成,我不需要在产品列表中添加任何内容。我只想在用户滚动时在页面中显示内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    相关资源
    最近更新 更多