最正确地保存RecyclerView的状态?

我在主要活动中有碎片包含recyclerview

Fragment.java

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreate(savedInstanceState); View view = inflater.inflate(R.layout.activity_main, container, false); initInstances(view); setRetainInstance(true); return view; } private void initInstances(View view) { mRecyclerView = (RecyclerView) view.findViewById(R.id.dialogList); mLayoutManager = new LinearLayoutManager(getActivity()); mRecyclerView.setLayoutManager(mLayoutManager); adapter = new MyAdapter(items); mRecyclerView.setAdapter(mAdapter); } 

MainActivity.java

在onCreate方法中:

 myFragment = (MyFragment) getSupportFragmentManager().findFragmentByTag(MyFragment.TAG); if (MyFragment == null) { MyFragment = new MyFragment(); getSupportFragmentManager().beginTransaction().add(R.id.content_frame, myFragment, MyFragment.TAG).commit(); } 

但是,当方向改变时,我的RecyclerView重绘。

我已经尝试保存状态与覆盖onSaveInstanceStateonRestoreInstanceState像这样如何防止自定义视图失去状态跨屏幕方向更改或保存和恢复视图状态机器人 ,但无济于事

如何在方向更改时正确保存RecyclerView和适配器项目的状态?

解决scheme:我的片段从一些方法扩展到BaseFragment:

 public class BaseFragment extends Fragment{ Bundle savedState; public BaseFragment() { super(); if (getArguments() == null) setArguments(new Bundle()); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (!restoreStateFromArguments()) { onFirstTimeLaunched(); } } protected void onFirstTimeLaunched() { } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); saveStateToArguments(); } public void saveStateToArguments() { if (getView() != null) savedState = saveState(); if (savedState != null) { Bundle b = getArguments(); b.putBundle("internalSavedViewState8954201239547", savedState); } } private boolean restoreStateFromArguments() { Bundle b = getArguments(); savedState = b.getBundle("internalSavedViewState8954201239547"); if (savedState != null) { onRestoreState(savedState); return true; } return false; } private Bundle saveState() { Bundle state = new Bundle(); onSaveState(state); return state; } protected void onRestoreState(Bundle savedInstanceState) { } protected void onSaveState(Bundle outState) { } @Override public void onStart() { super.onStart(); } @Override public void onStop() { super.onStop(); } @Override public void onDestroyView() { super.onDestroyView(); saveStateToArguments(); } 

在我的片段中,我覆盖方法并保存mLayoutManager的状态

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); } @Override protected void onSaveState(Bundle outState) { super.onSaveState(outState); outState.putParcelable("myState", mLayoutManager.onSaveInstanceState()); } @Override protected void onRestoreState(Bundle savedInstanceState) { super.onRestoreState(savedInstanceState); mLayoutManager.onRestoreInstanceState(savedInstanceState.getParcelable("myState")); } 

Solutions Collecting From Web of "最正确地保存RecyclerView的状态?"

我search了如何获得RecyclerView的滚动位置,但没有看到我可以找回它的位置,所以我认为最好的解决scheme是直接在你的java代码中处理旋转变化,并防止你的活动重新开始取向变化。

为了防止方向更改重新启动,可以按照此解决scheme 。

在阻止Activity重新启动之后,您可以构build2个xml布局文件,一个用于纵向模式,另一个用于横向模式,在这两个xml文件中添加一个FrameLayout,为您的RecyclerView保留位置。

当调用onConfigurationChange(Configuration newConfig)方法时,可以在新的方向之后使用适当的LayoutFile调用setContentView(int layoutFile),并在FrameLayout中添加您在类的成员中持有的RecyclerView。