在RecyclerView中保留滚动位置

时间:2016-09-15 16:40:56

标签: java android android-recyclerview scroll-position

我已经阅读了很多关于如何在设备旋转时保留RecyclerView中当前滚动位置的建议。我现在发现,当我使用com.android.support:recyclerview-v7:23.0.+时,当前位置实际保存在<{1}}中。它似乎也恢复了,但它只是“有点正确”恢复。

我正在经历这个:我以纵向模式滚动到列表底部,然后切换到横向。当前位置正确恢复(当然,有些项目不可见 - 我可以继续滚动)!

没有任何进一步的滚动,我旋转回到肖像,恢复的位置是我实际认为我已经拥有的位置上方的几个项目。 该列表不再滚动到底部

我的问题是:我还能做些什么来实际存储和恢复正确的滚动位置吗?

这是savedInstanceState

的布局
RecyclerView

这是单个项目的缩短布局:

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="...">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/itemsView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

</android.support.v4.widget.SwipeRefreshLayout>

托管<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="8dp" android:paddingBottom="8dp" android:paddingLeft="12dp" android:paddingRight="12dp"> <!-- This linear layout aligns type indicator to the left and text content to the right --> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> ... </LinearLayout> </LinearLayout> 的片段包含以下代码:

RecyclerView

1 个答案:

答案 0 :(得分:2)

事实证明,在更新到当前的API 24和最新的支持库之后,默认情况下一切都按预期工作,无需进一步编码......

相关问题