问题
When the text is changed, the layout refreshed and the scrolling in the recyclerview is resets to position 0, 0.
Here is my layout:
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView android:id="@+id/recyclerview1"
android:layout_width="match_parent"
android:layout_height="132dp"
android:scrollbars="horizontal" />
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<TextView android:text="@{prod.name}"
android:maxEms="12"
style="@style/ProdGroupLabel" />
<Button android:id="@+id/buttonedit"
android:text="Edit"
android:visibility="@{prod.hasOptions ? View.VISIBLE : View.INVISIBLE}"
style="@style/ProdGroupButtonEdit"/>
</LinearLayout>
</LinearLayout>
Now, when the product name has changed, it will appear in the text view <TextView android:text="@{prod.name}"
. This will force the recyclerview1
to reset its scroll position to 0,0.
Any idea on how to prevent the scrolling and preserve the original scrolling?
Thanks...
回答1:
You can make use of last visible position of layout manager you must be using before setting text
int position = manager.findLastVisibleItemPosition();
Then u can do like this
rv.getLayoutManager().scrollToPosition(poaition)
There is also
findLastCompletelyVisibleItemPosition you can make use of.
来源:https://stackoverflow.com/questions/54274599/onlayout-changed-causes-recyclerview-horizontal-scroll-to-reset