2016-11-14 65 views
2

我有3個活動: 甲 - >乙 - 「ç安卓:RecyclerView GridLayoutManager - 恢復滾動位置不正常工作

在活動B,我使用RecyclerView的GridlayoutManager填充。我要保存的滾動狀態,當我瀏覽到活動C和恢復的滾動狀態時,我從活動C.

private RecyclerView mImgList; 
private GridLayoutManager mRecyclerGridMan; 
private final String KEY_RECYCLER_STATE = "recycler_state"; 
private Parcelable mListState = null; 
private static Bundle mBundleRecyclerViewState; 

回到活動B。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

@Override 
protected void onPause() 
{ 
    super.onPause(); 
    mBundleRecyclerViewState = new Bundle(); 
    mListState = mImgList.getLayoutManager().onSaveInstanceState(); 
    mBundleRecyclerViewState.putParcelable(KEY_RECYCLER_STATE, mListState); 
} 

@Override 
protected void onResume() 
{ 
    super.onResume(); 
    if (mBundleRecyclerViewState != null) { 
     mListState = mBundleRecyclerViewState.getParcelable(KEY_RECYCLER_STATE); 
     mImgList.getLayoutManager().onRestoreInstanceState(mListState); 
    } 
} 

但是,當我從活動B按後退按鈕,進入到活動A,並從活動A.導航回活動B

+0

你有沒有檢查過你的'mBundleRecyclerViewState'確實不是'null'? –

+0

@VladMatvienko是的,我檢查了,當我從活動C回到活動B時,mBundleRecyclerViewState不爲空。 –

+0

你回到活動後再次填充你的列表嗎? –

回答

0

在onRestoreInstanceState恢復狀態()

protected void onRestoreInstanceState(Bundle state) { 
super.onRestoreInstanceState(state); 

// Retrieve list state and list/item positions 
if(state != null) 
    mListState = mBundleRecyclerViewState.getParcelable(KEY_RECYCLER_STATE); 
} 
這個工程
+0

我試過了,但沒有工作,甚至滾動狀態也沒有恢復,當導航到活動A到B,這是工作之前 –

+0

仍然不工作的兄弟 –

+0

以下是完整的代碼鏈接:https://github.com /zayid/Blur_Wallpaper/blob/master/app/src/main/java/com/whackyard/whatsappwalls/MainActivity.java –

相關問題