簡體   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