2016-09-20 26 views
2

私のアプリケーションのリストでページ番号を有効にするために、私はカスタムRecyclerViewを作った。私は状態を復元する際にクラッシュしています。おそらく、このクラッシュはサポートライブラリの既知のバグと関係しているかもしれませんが、サポートライブラリのバージョン24.0.0https://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; 
} 

そしてここではonSaveInstanceStateonRestoreInstanceStateですカスタム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 ...

ありがとうございました!

+0

代わりのBaseSaveState、recyclerviewでシンプルなオーバーライドonsaveInstanceStateとの延長をバンドルにあなたの必要な値を保存し、onrestoreinstancestateオーバーライドして、これらの値を復元 –

+0

しかし、CustomViewsでonSaveInstanceStateにバンドルを受け取っていない場合、データでカスタム状態クラスを作成し、それをonSaveInstanceStateでParcelableとして返す必要がありますか? – antonicg

答えて

0

最後に、AbsSavedStateを使用して解決しました(したがって、サポートライブラリバージョン24.2.1からSavedStateクラスを拡張しました)。以下とからParcelオブジェクトを受け取りconstructor変更:

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

へ:

public SavedState(Parcel source) { 
    super(source, LinearLayoutManager.class.getClassLoader()); 
    //... 
} 
関連する問題