我制作了自定义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。
以下是SavedState
控制器内的CustomRecyclerView
课程:
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中的onSaveInstanceState
和onRestoreInstanceState
实现:
@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);
}
我还尝试从支持SavedState
扩展AbsSavedState
并仍然崩溃。而且我不确定是否必须拨打onSaveInstanceState
的{{1}} ...
谢谢!
答案 0 :(得分:0)
最后,我解决了使用支持库版本24.2.1中的public Class Person{
private String person;
private String family;
private Map<String,Object> person_detail;
private Map<String,Object> address_detail;
getter...
setter...
}
(因此,我从中扩展了AbsSavedState
类)。并从以下位置更改接收SavedState
对象的constructor
:
Parcel
为:
public SavedState(Parcel source) {
super(source);
//...
}
答案 1 :(得分:0)
当我第一次尝试your solution时,我感到非常高兴,因为它确实有效。但是,当我查看其他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];
}
};