onLayout changed causes RecyclerView horizontal scroll to reset

▼魔方 西西 提交于 2019-12-13 21:15:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!