onLayout更改导致RecyclerView水平滚动重置 [英] onLayout changed causes RecyclerView horizontal scroll to reset
问题描述
更改文本后,布局将刷新,并且在recyclerview中的滚动将重置为位置0、0.
When the text is changed, the layout refreshed and the scrolling in the recyclerview is resets to position 0, 0.
这是我的布局:
<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>
现在,更改产品名称后,它将出现在文本视图<TextView android:text="@{prod.name}"
中.这将强制recyclerview1
将其滚动位置重置为0,0.
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?
谢谢...
推荐答案
您可以使用设置文本之前必须使用的布局管理器的最后一个可见位置
You can make use of last visible position of layout manager you must be using before setting text
int position = manager.findLastVisibleItemPosition();
那么你可以这样做
rv.getLayoutManager().scrollToPosition(poaition)
还有
您可以使用findLastCompletelyVisibleItemPosition .
这篇关于onLayout更改导致RecyclerView水平滚动重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!