保存状态自定义 RecyclerView

Saving state custom RecyclerView

我定制了 RecyclerView 以便在我的应用程序列表中启用分页。我在恢复状态时遇到了崩溃。也许这个崩溃与支持库中的一个已知错误有关,但是,理论上,它已经在支持库版本 24.0.0: https://code.google.com/p/android/issues/detail?id=196430 中得到解决,但它仍然崩溃。

也许我做错了什么?我正在 BadParcelableException ClassNotFoundException when unmarshalling: android.support.v7.widget.RecyclerView$SavedStateHere 尝试重新创建 SavedState。

这是我在 CustomRecyclerView 控制器中的 SavedState class:

public static class SavedState extends RecyclerView.BaseSavedState {

    public boolean isLastPage;
    public boolean isLoading;
    public int maxPages;
    public int pageSize;
    public int currentPage;
    public int firstVisibleItem;
    public Parcelable layoutManagerState;

    public SavedState(Parcelable superState) {
        super(superState);
    }

    public SavedState(Parcel source) {
        super(source);
        this.isLastPage = source.readByte() == 1;
        this.isLoading = source.readByte() == 1;
        this.maxPages = source.readInt();
        this.pageSize = source.readInt();
        this.currentPage = source.readInt();
        this.firstVisibleItem = source.readInt();
        this.layoutManagerState = source.readParcelable(LinearLayoutManager.SavedState.class.getClassLoader());
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeByte((byte) (isLastPage ? 1 : 0));
        dest.writeByte((byte) (isLoading ? 1 : 0));
        dest.writeInt(maxPages);
        dest.writeInt(pageSize);
        dest.writeInt(currentPage);
        dest.writeInt(firstVisibleItem);
        dest.writeParcelable(layoutManagerState, flags);
    }

    public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
        @Override
        public SavedState createFromParcel(Parcel source) {
            return new SavedState(source);
        }

        @Override
        public SavedState[] newArray(int size) {
            return new SavedState[size];
        }
    };
}

这是我在控制器中保存和恢复状态的地方:

public Parcelable saveState(Parcelable superState) {
    SavedState savedState       = new SavedState(superState);
    savedState.isLastPage       = this.isLastPage;
    savedState.isLoading        = this.isLoading;
    savedState.maxPages         = this.maxPages;
    savedState.pageSize         = this.pageSize;
    savedState.currentPage      = this.currentPage;
    savedState.firstVisibleItem = this.firstVisibleItemPosition;

    if (layoutManager != null) {
        savedState.layoutManagerState = layoutManager.onSaveInstanceState();
    }

    return savedState;
}

public Parcelable restoreState(SavedState savedState) {

    this.isLastPage                 = savedState.isLastPage;
    this.isLoading                  = savedState.isLoading;
    this.maxPages                   = savedState.maxPages;
    this.pageSize                   = savedState.pageSize;
    this.currentPage                = savedState.currentPage;
    this.firstVisibleItemPosition   = savedState.firstVisibleItem;

    return savedState.layoutManagerState;
}

这里是自定义 RecyclerView 中的 onSaveInstanceStateonRestoreInstanceState 实现:

    @Override
    protected Parcelable onSaveInstanceState() {

        return controller.saveState(super.onSaveInstanceState());
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {

        if (state instanceof EndlessRecyclerViewController.SavedState) {

            EndlessRecyclerViewController.SavedState savedState = (EndlessRecyclerViewController.SavedState) state;

            Parcelable layoutManagerState = controller.restoreState(savedState);

            getLayoutManager().onRestoreInstanceState(layoutManagerState);
            super.onRestoreInstanceState(savedState.getSuperState());
        }
        else
            super.onRestoreInstanceState(state);
    }

我还尝试从支持 AbsSavedState 扩展 SavedState,但仍然崩溃。而且我不确定我是否必须调用 LinearLayoutManageronSaveInstanceState...

谢谢!

最后,我使用支持库版本 24.2.1 中的 AbsSavedState(因此,我从中扩展 SavedState class)解决了问题。并更改从以下位置接收 Parcel 对象的 constructor

public SavedState(Parcel source) {
  super(source);
  //... 
}

至:

public SavedState(Parcel source) {
  super(source, LinearLayoutManager.class.getClassLoader());
  //... 
}

当我第一次尝试 I was quite happy because it actually worked. However when I was looking at some other Android code 时,我发现有更好的解决方案。您需要添加一个新的构造函数并从创建者那里调用它。

// add new constructor
@RequiresApi(Build.VERSION_CODES.N)
public SavedState(Parcel source, ClassLoader loader) {
    super(source, loader);
    this.isLastPage = source.readByte() == 1;
    this.isLoading = source.readByte() == 1;
    this.maxPages = source.readInt();
    this.pageSize = source.readInt();
    this.currentPage = source.readInt();
    this.firstVisibleItem = source.readInt();
    this.layoutManagerState = source.readParcelable(LinearLayoutManager.SavedState.class.getClassLoader());
}

public static final Creator<SavedState> CREATOR = new ClassLoaderCreator<SavedState>() {

    // add createFromParcel method with ClassLoader
    @Override
    public SavedState createFromParcel(Parcel source, ClassLoader loader)
    {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? new SavedState(source, loader) : new SavedState(source);
    }

    @Override
    public SavedState createFromParcel(Parcel source)
    {
        // call other createFromParcel method.    
        return createFromParcel(source, null);
    }

    @Override
    public SavedState[] newArray(int size) {
        return new SavedState[size];
    }
};