私のアプリケーションのリストでページ番号を有効にするために、私はカスタムRecyclerView
を作った。私は状態を復元する際にクラッシュしています。おそらく、このクラッシュはサポートライブラリの既知のバグと関係しているかもしれませんが、サポートライブラリのバージョン24.0.0
:https://code.google.com/p/android/issues/detail?id=196430で解決されていますが、まだクラッシュしています。状態の保存RecyclerView
多分私は何か間違っていますか?私はBadParcelableException ClassNotFoundException when unmarshalling: android.support.v7.widget.RecyclerView$SavedStateHere
SavedStateを再作成しようとしています。ここで
は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;
}
そしてここではonSaveInstanceState
とonRestoreInstanceState
ですカスタムRecyclerViewの実装:
@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
から延長しようとしましたが、依然としてクラッシュしました。電話しなければならないかどうかわかりませんLinearLayoutManager
...
ありがとうございました!
代わりのBaseSaveState、recyclerviewでシンプルなオーバーライドonsaveInstanceStateとの延長をバンドルにあなたの必要な値を保存し、onrestoreinstancestateオーバーライドして、これらの値を復元 –
しかし、CustomViewsでonSaveInstanceStateにバンドルを受け取っていない場合、データでカスタム状態クラスを作成し、それをonSaveInstanceStateでParcelableとして返す必要がありますか? – antonicg