gpt4 book ai didi

android - 保存状态自定义 RecyclerView

转载 作者:行者123 更新时间:2023-12-02 03:11:51 26 4
gpt4 key购买 nike

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

也许我做错了什么?解码时出现 BadParcelableException ClassNotFoundException:android.support.v7.widget.RecyclerView$SavedStateHere 试图重新创建 SavedState。

这是我在 CustomRecyclerView Controller 中的 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];
}
};
}

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

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 类)解决了问题。并更改从以下位置接收 Parcel 对象的 constructor:

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

到:

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

关于android - 保存状态自定义 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39589460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com