RecyclerView with reverseLayout "jumps" upon notifyDataSetChanged

612 Views Asked by At

I set my recyclerView's layoutManager like this:

recyclerView.layoutManager = LinearLayoutManager(context, HORIZONTAL, true)

The last parameter sets the reverseLayout to true which means it will be loaded from right to left. I need this because I implement a sort of calendar where the last date is current date but you can scroll into the past infinitely.

The problem is that when in my adapter I call notifyItemChanged or notifyDataSetChanged the list scrolls a few pixels to the right. This doesn't happen with reverseLayout=false.

It also only happens while the size of the RecyclerView is MATCH_PARENT, when I set it manually to whatever dp, the problem is gone.

Any ideas what happens and how to cope with it?

2

There are 2 best solutions below

0
On

Setting RecyclerView dimensions programmatically worked for me.

In kotlin -

constraintLayout.doOnLayout {
    recyclerview.layoutParams.height = bottomLayout.top - appBarLayout.bottom
}

xml -

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toTopOf="@id/bottomLayout" />

Where I want recyclerView between bottomLayout and appBarLayout and constraintLayout is root layout.

1
On

Maybe it is too late, but anyway, I'm leaving this here. Using constraint layout with match constraints on recyclerview (0dp with horizontal constraints) solved the problem for me.