java - 在 RecyclerView 中保留滚动位置

标签 java android android-recyclerview scroll-position

我读过很多关于如何在设备旋转时在 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 的 fragment 具有以下代码:

@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;
}

最佳答案

事实证明,在更新到当前的 API 24 和最新的支持库后,默认情况下一切正常,无需进一步编码......

关于java - 在 RecyclerView 中保留滚动位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39516266/

相关文章:

java - 如何将java泛型翻译成Delphi

java - 递归定义中的堆栈溢出错误

android - WEB_CALLBACK_URI 与预期的 URI 不匹配 Square POS API

安卓双卡支持

android - 使用 FlexboxLayoutManager 将 RecyclerView 项目居中

java - 从 ArrayList 中移除项目,但 notifyDataSetChanged() 仅删除底部项目

java - Spring mongo 线程安全

java - netty中的channelActive和channelRead有什么区别?

android - 避免本地化资源开销

android - 如何在具有不同类型消息的聊天应用程序上组织 RecyclerView?