在RecyclerView中保留滚动位置

3
我已经阅读了很多关于在设备旋转时如何保留RecyclerView中当前滚动位置的建议。我现在发现,当我使用com.android.support:recyclerview-v7:23.0.+时,当前位置实际上被保存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的片段具有以下代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View v = inflater.inflate(R.layout.calendar_fragment, container, false);
    ButterKnife.bind(this, v);

    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            ...
        }
    });

    if (mLayoutManager == null)
    {
        mLayoutManager = new LinearLayoutManager(getActivity());
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    }

    // Get cached information or access storage
    if (savedInstanceState != null)
    {
        // Initialize the area and the adapter from the saved state
        mArea = savedInstanceState.getParcelable("AREA");
        Entry[] entries = (Entry[])savedInstanceState.getParcelableArray("ITEMS");
        mDataAdapter = new MyAdapter(this, mArea, entries);
    }
    else
    {
        // Initialize the area and the adapter form scratch
        mArea = getAreaByPreference();
        mDataAdapter = new MyAdapter(this, mArea);
    }

    // Initialize the RecyclerView
    mItemsView.setHasFixedSize(true);
    mItemsView.setAdapter(mDataAdapter);
    mItemsView.setLayoutManager(mLayoutManager);

    return v;
}

你尝试过保存位置,然后使用 void scrollToPosition (int position) 吗?https://developer.android.com/reference/android/support/v7/widget/LinearLayoutManager.html#scrollToPosition(int) - Jonsmoke
你看过这个答案吗?https://dev59.com/Cl4c5IYBdhLWcg3wiKyU#29166336 - Jonsmoke
是的,这是我尝试过的其中一种。我会再试一次,以防我做错了什么... - Thorsten Dittmar
正如我所说 - 无论是在Jonsmoke链接的问题的答案中,还是在默认行为中,当从纵向模式切换到横向模式并返回时,都没有恢复正确的位置。虽然在横向模式下保留了位置,但在返回纵向模式时滚动到了错误的位置。 - Thorsten Dittmar
1
顺便说一下,你尝试的这个答案:https://dev59.com/Cl4c5IYBdhLWcg3wiKyU#29166336 完全滥用了 API,我不知道它是如何得到 79 个赞的。 - pskink
显示剩余5条评论
1个回答

2
原来,在更新到当前API 24和最新的支持库之后,一切都可以按照默认设置正常工作,无需进一步编码...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接