繁体   English   中英

无法使用LayoutManager在RecyclerView中还原滚动位置

[英]Unable to Restore the Scroll Position in RecyclerView using LayoutManager

因此,我有一个EditText,在其上设置了onEditorActionListener,即,在用户输入文本并按Enter / Search后,它将获取详细信息并相应地填充回收者视图。

现在,为了保存配置更改的状态,我编写了以下代码-

Parcelable stateList;

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    //Saving instance of the state in Parcelable stateList
    stateList = recyclerView.getLayoutManager().onSaveInstanceState();
    outState.putParcelable(RETAIN_STATE, stateList);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    if(savedInstanceState!=null) {
        stateList = savedInstanceState.getParcelable(RETAIN_STATE);
        recyclerView.getLayoutManager().onRestoreInstanceState(stateList);
    }
}

但是,当我运行它并旋转屏幕时,回收者视图不会从可拆分的stateList中恢复状态。

我正在使用MVP,因此在演示者的回调中设置适配器。

旋转屏幕后,当我们单击键盘上的enter / search时,我能够保留状态,因此我在onRestoreInstanceState()中尝试了此技巧,但我认为应该有更好的方法来解决此问题。

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    if(savedInstanceState!=null) {
        //The hack!
        et_search.onEditorAction(EditorInfo.IME_ACTION_SEARCH);
        stateList = savedInstanceState.getParcelable(RETAIN_STATE);
        recyclerView.getLayoutManager().onRestoreInstanceState(stateList);
    }
}

让我知道是否需要更多信息。 提前致谢。

您实际上不需要在LayoutManager中进行配置更改时调用onRestoreInstanceState ,因为它将被自动调用(所有视图都具有从生命周期所有者传递的onSaveInstanceStateonRestoreInstanceState )。 即使您愿意-所要做的就是恢复视图的滚动位置。

您实际需要做的是保存在RecyclerView适配器中设置/使用的数据,并在旋转屏幕时将其重新设置。 然后再次调用搜索(或过滤器,无论如何)。

如果您不想通过覆盖配置更改来定制任何东西,可以让android为您处理方向更改。例如,隐藏一些视图等

<activity android:name=".mainpage.view.MainActivity" android:configChanges="orientation|screenSize|screenLayout" >

将此内容包含在该活动的清单中,进一步在旋转时在recyler视图中保存滚动位置,您可以使用以下要点来避免重复代码,因为可以在需要相同功能的项目中使用此自定义recyclerview

 import android.content.Context; import android.os.Bundle; import android.os.Parcelable; import android.support.annotation.Nullable; import android.support.v7.widget.RecyclerView; import android.util.AttributeSet; /** * Class {@link StatefulRecyclerView} extends {@link RecyclerView} and adds position management on configuration changes. * * @author FrantisekGazo * @version 2016-03-15 */ public final class StatefulRecyclerView extends RecyclerView { private static final String SAVED_SUPER_STATE = "super-state"; private static final String SAVED_LAYOUT_MANAGER = "layout-manager-state"; private Parcelable mLayoutManagerSavedState; public StatefulRecyclerView(Context context) { super(context); } public StatefulRecyclerView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public StatefulRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected Parcelable onSaveInstanceState() { Bundle bundle = new Bundle(); bundle.putParcelable(SAVED_SUPER_STATE, super.onSaveInstanceState()); bundle.putParcelable(SAVED_LAYOUT_MANAGER, this.getLayoutManager().onSaveInstanceState()); return bundle; } @Override protected void onRestoreInstanceState(Parcelable state) { if (state instanceof Bundle) { Bundle bundle = (Bundle) state; mLayoutManagerSavedState = bundle.getParcelable(SAVED_LAYOUT_MANAGER); state = bundle.getParcelable(SAVED_SUPER_STATE); } super.onRestoreInstanceState(state); } /** * Restores scroll position after configuration change. * <p> * <b>NOTE:</b> Must be called after adapter has been set. */ private void restorePosition() { if (mLayoutManagerSavedState != null) { this.getLayoutManager().onRestoreInstanceState(mLayoutManagerSavedState); mLayoutManagerSavedState = null; } } @Override public void setAdapter(Adapter adapter) { super.setAdapter(adapter); restorePosition(); } } 

链接到要点

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM