在RecyclerView中保留滚动位置
问题描述:
我已阅读了关于如何在设备旋转时保留RecyclerView
中当前滚动位置的许多建议。我现在发现当我用com.android.support:recyclerview-v7:23.0.+
时,当前位置是实际上保存在中的savedInstanceState
。它似乎也恢复了,但它只是“有点正确地”恢复。在RecyclerView中保留滚动位置
我遇到了这个问题:我在纵向模式下滚动到列表的底部,然后切换到横向。当前位置恢复正确(当然,有些项目不可见 - 我可以继续滚动)!
没有任何进一步的滚动,我旋转回肖像和恢复的位置是我实际以为我会有的位置以上的几个项目。 该列表不再滚动到底部。
我的问题是:还有什么我可以做的实际存储和恢复正确的滚动位置?
这是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;
}
答
原来,更新后到目前的API 24和最新的支持库一切都按需要默认工作,没有进一步的编码...
你试过保存位置,然后使用'void scrollToPosition(int position)'? https://developer.android.com/reference/android/support/v7/widget/LinearLayoutManager.html#scrollToPosition(int) – Jonsmoke
不,我还没有尝试过,因为我找到的所有其他解决方案建议让'LinearLayoutManager'做它的事情挽救和恢复自己的状态。 –
你见过这个答案吗? http://*.com/a/29166336/3658819 – Jonsmoke