I'm developing a application that the RecyclerView load new data when the user hit the bottom of the scroll. However, the list is already completed and have it's fixed size, I just want to put the content in the Recycler, but when I insert the content in the Recycler, the scroll returns to the top of the list. And then I need to scroll down all over again until I reach the new data that was inserted in to the bottom of the list. All that I want is to maintain the scroll position when the new data is loaded.
I've tried use descendantFocusability = "blocksDescendants", but this didn't work.
This is my XML code where the RecyclerView can be found
<?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>
The code that is used to add data to the RecyclersView is
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
}
}
My 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
}
}
All application works really fine, the only problem is with that scroll behavior. I expected that the scroll position didn't goes to the top of the RecyclersView list when I add new data in him.