问题
I encountered an issue with RecyclerView
when it is a child of a ConstraintLayout
. Originally, the RecyclerView
was somewhere deep in the view hierarchy of Relative-
, Frame-
and LinearLeayout
s, and everything worked well until I decided to flatten view tree using ConstraintLayout. I noticed that each time when there is a layout change, caused by anything (window resize, data set change notification, etc), the scroll position of the RecyclerView
is changed.
For example, every time I show and hide soft keyboard, the content recycler view drifts by a certain amount of pixels.
I was able to reproduce this behavior on a very simple layout:
ConstraintLayout
|---RecyclerView
|---Button
|---EditText
If I set constraints such that RecyclerView
is placed above any other child of ConstraintLayout
(meaning RecyclerView has a bottom constraint), and if I use LinearLayoutManager
with reverseLayout = true
, I can reproduce the described above bahavior.
How should I fix this (I don't want the scroll position to change)? Maybe there are some flags on RecyclerView and/or ConstraintLayout that I don't know of...
Here is my layout.xml
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintTop_toTopOf="parent"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notify Items Changed"
app:layout_constraintBottom_toTopOf="@+id/edit_text"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintStart_toEndOf="parent"
/>
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
/>
</android.support.constraint.ConstraintLayout>
And here is code to setup RecyclerView
in Activity.onCreate()
:
setContentView(R.layout.activity_main)
val adapter = Adapter()
val layoutManager = LinearLayoutManager(this)
layoutManager.orientation = LinearLayoutManager.VERTICAL
layoutManager.reverseLayout = true
val recycler = findViewById<RecyclerView>(R.id.recycler)
recycler.layoutManager = layoutManager
recycler.adapter = adapter
recycler.addOnLayoutChangeListener { _, _, _, _, bottom, _, _, _, oldBottom ->
Log.d("TEST", "bottom=$bottom, oldBottom=$oldBottom")
}
recycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
Log.d("TEST", "Scrolled: dx=$dx, dy=$dy")
Log.d("TEST", "offset = " + recycler.computeVerticalScrollOffset())
}
})
findViewById<Button>(R.id.button)
.setOnClickListener {
adapter.notifyDataSetChanged()
}
Here is a fragment of debug logs, which were collected after several clicks on the button:
D/TEST: Scrolled: dx=0, dy=0
D/TEST: offset = 4167
D/TEST: bottom=1253, oldBottom=1253
D/TEST: Scrolled: dx=0, dy=0
D/TEST: offset = 3887
D/TEST: bottom=1253, oldBottom=1253
D/TEST: Scrolled: dx=0, dy=0
D/TEST: offset = 3607
D/TEST: bottom=1253, oldBottom=1253
D/TEST: Scrolled: dx=0, dy=0
D/TEST: offset = 3327
D/TEST: bottom=1253, oldBottom=1253
The scroll positions is slowly changing and content is being scrolled towards the top. The shift is roughly amounts to the total height of widgets below RecyclerView
.
I could reproduce this behavior only when all conditions are met:
RecyclerView
is positioned inside aConstraintLayout
- There are other sibling widgets below
RecyclerView
LinearLayoutManager
is used with either of flagsreverseLayout
orstackFromBottom
being set totrue
.
I am using ConstraintLayout
version is 1.1.2
.
来源:https://stackoverflow.com/questions/52487038/recyclerview-changes-its-scroll-position-when-inside-a-constraintlayout