保存RecyclerView状态的最佳方法是什么?

时间:2015-04-24 06:19:35

标签: android android-fragments android-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,例如How to prevent custom views from losing state across screen orientation changesSaving and restoring view state android,但对我无效

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

解: 我的片段使用一些方法从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"));
}

1 个答案:

答案 0 :(得分:-1)

我搜索了如何获取RecyclerView的滚动位置,但没有看到我可以检索它的位置,所以我认为最好的解决方案是直接在java代码中处理轮换更改,并阻止您的活动重新启动方向改变。

要防止重定向更改,您可以关注this solution

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

当调用onConfigurationChange(Configuration newConfig)方法时,使用新方向后面的相应LayoutFile调用setContentView(int layoutFile),并在FrameLayout中添加您在类成员中保存的RecyclerView。