Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Implement scroll position saved state for StickyHeaderLayoutManager
Browse files Browse the repository at this point in the history
  • Loading branch information
ShamylZakariya committed Jun 8, 2016
1 parent 7461a59 commit dc63a98
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
Expand All @@ -21,6 +22,7 @@
public class DemoActivity extends AppCompatActivity {

private static final String TAG = DemoActivity.class.getSimpleName();
private static final String STATE_SCROLL_POSITION = "DemoActivity.STATE_SCROLL_POSITION";

RecyclerView recyclerView;
ProgressBar progressBar;
Expand All @@ -44,7 +46,25 @@ public void onClick(View v) {
}
});
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
RecyclerView.LayoutManager lm = recyclerView.getLayoutManager();
Parcelable scrollState = lm.onSaveInstanceState();
outState.putParcelable(STATE_SCROLL_POSITION, scrollState);
super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState != null) {
Parcelable scrollPositionState = savedInstanceState.getParcelable(STATE_SCROLL_POSITION);
if (scrollPositionState != null) {
recyclerView.getLayoutManager().onRestoreInstanceState(scrollPositionState);
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.content.Context;
import android.graphics.PointF;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
Expand Down Expand Up @@ -70,6 +72,8 @@ public interface HeaderPositionChangedCallback {
// adapter position (iff >= 0) of the item selected in scrollToPosition
int scrollTargetAdapterPosition = -1;

SavedState pendingSavedState;


public StickyHeaderLayoutManager() {
}
Expand Down Expand Up @@ -111,12 +115,45 @@ public void onDetachedFromWindow(RecyclerView view, RecyclerView.Recycler recycl
adapter = null;
}

@Override
public Parcelable onSaveInstanceState() {
if (pendingSavedState != null) {
Log.d(TAG, "onSaveInstanceState: returning existing unused saved state");
return pendingSavedState;
}

updateFirstAdapterPosition();
SavedState state = new SavedState();
state.firstViewAdapterPosition = firstViewAdapterPosition;
state.firstViewTop = firstViewTop;

Log.d(TAG, "onSaveInstanceState: created saved state: " + state);

return state;
}

@Override
public void onRestoreInstanceState(Parcelable state) {
if (state instanceof SavedState) {
pendingSavedState = (SavedState)state;
Log.d(TAG, "onRestoreInstanceState: received saved state: " + pendingSavedState);
requestLayout();
} else {
Log.e(TAG, "onRestoreInstanceState: invalid saved state class, expected: " + SavedState.class.getCanonicalName() + " got: " + state.getClass().getCanonicalName() );
}
}

@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {

if (scrollTargetAdapterPosition >= 0) {
firstViewAdapterPosition = scrollTargetAdapterPosition;
firstViewTop = 0;
} else if (pendingSavedState != null && pendingSavedState.isValid()) {
Log.d(TAG, "onLayoutChildren: pendingSavedState present: " + pendingSavedState);
firstViewAdapterPosition = pendingSavedState.firstViewAdapterPosition;
firstViewTop = pendingSavedState.firstViewTop;
pendingSavedState = null; // we're done with saved state now
} else {
updateFirstAdapterPosition();
}
Expand Down Expand Up @@ -198,7 +235,6 @@ public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State
// put headers in sticky positions if necessary
updateHeaderPositions(recycler);
}

}

/**
Expand Down Expand Up @@ -395,6 +431,7 @@ public void scrollToPosition(int position) {
}

scrollTargetAdapterPosition = position;
pendingSavedState = null;
requestLayout();
}

Expand All @@ -404,9 +441,11 @@ public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State
throw new IndexOutOfBoundsException("adapter position out of range");
}

pendingSavedState = null;

// see: https://blog.stylingandroid.com/scrolling-recyclerview-part-3/
View firstVisibleChild = recyclerView.getChildAt(0);
int itemHeight = getItemHeightForSmoothScroll(recyclerView);
int itemHeight = getEstimatedItemHeightForSmoothScroll(recyclerView);
int currentPosition = recyclerView.getChildAdapterPosition(firstVisibleChild);
int distanceInPixels = Math.abs((currentPosition - position) * itemHeight);
if (distanceInPixels == 0) {
Expand All @@ -419,7 +458,7 @@ public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State
startSmoothScroll(scroller);
}

protected int getItemHeightForSmoothScroll(RecyclerView recyclerView) {
protected int getEstimatedItemHeightForSmoothScroll(RecyclerView recyclerView) {
int height = 0;
for (int i = 0, n = recyclerView.getChildCount(); i < n; i++) {
height = Math.max(getDecoratedMeasuredHeight(recyclerView.getChildAt(i)), height);
Expand Down Expand Up @@ -690,7 +729,7 @@ int getViewAdapterPosition(View view) {
}

// https://blog.stylingandroid.com/scrolling-recyclerview-part-3/
private class SmoothScroller extends LinearSmoothScroller {
class SmoothScroller extends LinearSmoothScroller {
private static final int TARGET_SEEK_SCROLL_DISTANCE_PX = 10000;
private static final float DEFAULT_DURATION = 1000;
private final float distanceInPixels;
Expand All @@ -715,4 +754,59 @@ protected int calculateTimeForScrolling(int dx) {
return (int) (duration * proportion);
}
}

static class SavedState implements Parcelable {

int firstViewAdapterPosition = RecyclerView.NO_POSITION;
int firstViewTop = 0;

public SavedState() {
}

SavedState(Parcel in) {
firstViewAdapterPosition = in.readInt();
firstViewTop = in.readInt();
}

public SavedState(SavedState other) {
firstViewAdapterPosition = other.firstViewAdapterPosition;
firstViewTop = other.firstViewTop;
}

boolean isValid() {
return firstViewAdapterPosition >= 0;
}

void invalidate() {
firstViewAdapterPosition = RecyclerView.NO_POSITION;
}

@Override
public String toString() {
return "<" + this.getClass().getCanonicalName() + " firstViewAdapterPosition: " + firstViewAdapterPosition + " firstViewTop: " + firstViewTop + ">";
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(firstViewAdapterPosition);
dest.writeInt(firstViewTop);
}

public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
@Override
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}

@Override
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
}

0 comments on commit dc63a98

Please sign in to comment.