-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
57 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 32 additions & 6 deletions
38
...pter/src/main/java/com/github/vivchar/rendererrecyclerviewadapter/CompositeViewState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,49 @@ | ||
package com.github.vivchar.rendererrecyclerviewadapter; | ||
|
||
import android.os.Parcelable; | ||
import android.support.annotation.NonNull; | ||
import android.support.v7.widget.LinearLayoutManager; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.util.Log; | ||
import android.view.View; | ||
|
||
import java.io.Serializable; | ||
import java.util.HashMap; | ||
|
||
/** | ||
* Created by Vivchar Vitaly on 20.10.17. | ||
* | ||
*/ | ||
public class CompositeViewState <VH extends CompositeViewHolder> implements ViewState<VH> { | ||
public class CompositeViewState <VH extends CompositeViewHolder> implements ViewState<VH>, Serializable { | ||
|
||
@NonNull | ||
protected final Parcelable mLayoutManagerState; | ||
protected HashMap<Integer, ViewState> mViewStates; | ||
protected int mPosition; | ||
protected int mTopOffset; | ||
protected int mLeftOffset; | ||
|
||
public <VH extends CompositeViewHolder> CompositeViewState(@NonNull final VH holder) { | ||
mLayoutManagerState = holder.getRecyclerView().getLayoutManager().onSaveInstanceState(); | ||
mViewStates = holder.getAdapter().getStates(); | ||
final RecyclerView.LayoutManager layoutManager = holder.getRecyclerView().getLayoutManager(); | ||
if (layoutManager instanceof LinearLayoutManager) { | ||
/* To get rid of Parcelable, https://stackoverflow.com/a/35287828/4894238 */ | ||
mPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition(); | ||
final View item = holder.getRecyclerView().getChildAt(0); | ||
mTopOffset = (item == null) ? 0 : (item.getTop() - holder.getRecyclerView().getPaddingTop()); | ||
mLeftOffset = (item == null) ? 0 : (item.getLeft() - holder.getRecyclerView().getPaddingLeft()); | ||
Log.d("###", "save mPosition: " + mPosition + " mTopOffset: " + mTopOffset); | ||
} else { | ||
mPosition = 0; | ||
mTopOffset = 0; | ||
} | ||
} | ||
|
||
@Override | ||
public void restore(@NonNull final VH holder) { | ||
holder.getRecyclerView().getLayoutManager().onRestoreInstanceState(mLayoutManagerState); | ||
holder.getAdapter().setStates(mViewStates); | ||
|
||
final RecyclerView.LayoutManager layoutManager = holder.getRecyclerView().getLayoutManager(); | ||
if (mPosition != -1 && layoutManager instanceof LinearLayoutManager) { | ||
Log.d("###", "restore mPosition: " + mPosition + " mTopOffset: " + mTopOffset); | ||
((LinearLayoutManager) layoutManager).scrollToPositionWithOffset(mPosition, Math.max(mTopOffset, mLeftOffset)); | ||
} | ||
} | ||
} |
Can't you just use the
savedInstanceState
bundle inonCreateView()
?