无法使用LayoutManager恢复RecyclerView中的滚动位置

问题描述:

因此,我有一个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); 
    } 
} 

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

你可以让机器人为您处理方向的变化,如果不通过重写配置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

因为它会自动调用(所有视图都具有从生命周期所有者传递的onSaveInstanceStateonRestoreInstanceState),所以您无需在LayoutManager中更改配置时调用onRestoreInstanceState。即使你想 - 所有你会实现这个是恢复视图的滚动位置。

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

+0

是的,我正在寻找恢复的意见滚动位置。 – hsm59