保存状态的自定义 RecyclerView

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

也许我做错了什么? 当试图重新创建SavedState时,我遇到了BadParcelableException ClassNotFoundException when unmarshalling: android.support.v7.widget.RecyclerView$SavedStateHere的问题。

以下是CustomRecyclerView控制器中的SavedState类:

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,但仍然出现错误。我不确定是否需要调用LinearLayoutManager的onSaveInstanceState...谢谢!

不需要扩展BaseSaveState,只需在RecyclerView中覆盖onsaveInstanceState并将所需的值保存在Bundle中,然后覆盖onrestoreinstancestate并恢复这些值。 - Akshay Bhat 'AB'
但是在自定义视图中,您无法在onSaveInstanceState中接收Bundle,您必须创建一个带有数据的自定义状态类,并将其作为Parcelable返回到onSaveInstanceState中,我说得对吗? - antonicg
2个回答

1
最后,我使用了支持库版本24.2.1中的AbsSavedState(因此,我从中扩展了SavedState类)。并且将接收Parcel对象的constructor进行了更改,如下所示:
public SavedState(Parcel source) {
  super(source);
  //... 
}

to:

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

1
当我第一次尝试你的解决方案时,我非常高兴,因为它确实起作用了。然而,当我查看其他Android代码时,我发现有一个更好的解决方案。您需要添加一个新的构造函数并从创建者中调用它。
// 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];
    }
};

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接