2017-07-31 88 views
0

因此,我有一個EditText,我在其上設置了onEditorActionListener,即在用戶輸入文本並按下enter/search之後,它將獲取詳細信息並相應地填充回收視圖。無法使用LayoutManager恢復RecyclerView中的滾動位置

現在爲了節省配置更改的狀態我已經寫了下面的代碼 -

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狀態parcelable。

我使用MVP,所以我在演示者的回調中設置適配器。

當我們點擊屏幕旋轉後的鍵盤上的輸入/搜索時,我能夠保留狀態,所以我在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); 
    } 
} 

讓我知道是否有更多所需的信息。 在此先感謝。

回答

0

你可以讓機器人爲您處理方向的變化,如果不通過重寫配置change.eg隱藏一些看法等

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

想要的任何自定義的東西,在這包括你的該活動的清單 爲了保存循環中的循環視圖中的滾動位置,您可以使用以下要點來避免重複的代碼,因爲您可以在項目中使用此自定義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(); 
 
    } 
 
}

Link to gist

0

因爲它會自動調用(所有視圖都具有從生命週期所有者傳遞的onSaveInstanceStateonRestoreInstanceState),所以您無需在LayoutManager中更改配置時調用onRestoreInstanceState。即使你想 - 所有你會實現這個是恢復視圖的滾動位置。

您實際上需要做的是保存數據集/在RecyclerView適配器中使用,並在旋轉屏幕時將其設回。然後再次調用搜索(或過濾器)。

+0

是的,我正在尋找恢復的意見滾動位置。 – hsm59

相關問題