Skip to content

Commit

Permalink
Keep selection position when calling dataSetChanged
Browse files Browse the repository at this point in the history
  • Loading branch information
BCsl committed May 23, 2017
1 parent 1960fbb commit 72b6e3a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -126,10 +127,15 @@ public void onClick(View view) {
mMainRecycle2.smoothScrollToPosition(selectPosition);
} else {
if (mMainRecycle1.getAdapter() instanceof DemoAdapter) {
((DemoAdapter) mMainRecycle1.getAdapter()).addData();
int result = ((DemoAdapter) mMainRecycle1.getAdapter()).dataChange();
if (result == 1) {
Toast.makeText(getContext(), "add data", Toast.LENGTH_SHORT).show();
} else if (result == -1) {
Toast.makeText(getContext(), "remove data", Toast.LENGTH_SHORT).show();
}
}
if (mMainRecycle2.getAdapter() instanceof DemoAdapter) {
((DemoAdapter) mMainRecycle2.getAdapter()).addData();
((DemoAdapter) mMainRecycle2.getAdapter()).dataChange();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.widget.TextView;

import java.util.List;
import java.util.Random;

import github.hellocsl.gallerylayoutmanager.BuildConfig;
import github.hellocsl.gallerylayoutmanager.R;
Expand Down Expand Up @@ -43,6 +44,29 @@ public void addData() {
}
}

private static final Random RANDOM = new Random();

public int dataChange() {
int result = 0;
if (mItems != null) {
if (RANDOM.nextBoolean()) {
for (int i = 0; i < 10; i++) {
mItems.add("Extra:" + i);
}
result = 1;
} else {
int size = mItems.size();
int cut = size / 2;
for (int i = size - 1; i > cut; i--) {
mItems.remove(i);
}
result = -1;
}
notifyDataSetChanged();
}
return result;
}

public DemoAdapter setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.mOnItemClickListener = onItemClickListener;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
* Created by chensuilun on 2016/11/18.
*/
public class GalleryLayoutManager extends RecyclerView.LayoutManager implements RecyclerView.SmoothScroller.ScrollVectorProvider {
private static final boolean DEBUG = false;
private static final String TAG = "GalleryLayoutManager";
final static int LAYOUT_START = -1;

Expand All @@ -33,7 +32,7 @@ public class GalleryLayoutManager extends RecyclerView.LayoutManager implements

private int mFirstVisiblePosition = 0;
private int mLastVisiblePos = 0;
private int mInitialSelectedPosition = -1;
private int mInitialSelectedPosition = 0;

int mCurSelectedPosition = -1;

Expand Down Expand Up @@ -104,7 +103,7 @@ public boolean checkLayoutParams(RecyclerView.LayoutParams lp) {

@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "onLayoutChildren() called with: state = [" + state + "]");
}
if (getItemCount() == 0) {
Expand All @@ -124,40 +123,42 @@ public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State
if (getChildCount() == 0 || state.didStructureChange()) {
reset();
}
mInitialSelectedPosition = Math.min(Math.max(0, mInitialSelectedPosition), getItemCount() - 1);
detachAndScrapAttachedViews(recycler);
firstFillCover(recycler, state, 0);
}


private void reset() {
if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "reset: ");
}
if (mState != null) {
mState.mItemsFrames.clear();
}
if (mCurSelectedPosition != -1) {
mInitialSelectedPosition = mCurSelectedPosition;
} else {
mInitialSelectedPosition = 0;
}
mFirstVisiblePosition = mInitialSelectedPosition;
mLastVisiblePos = mInitialSelectedPosition;
mCurSelectedPosition = -1;
mFirstVisiblePosition = 0;
if (mCurSelectedView != null) {
mCurSelectedView.setSelected(false);
mCurSelectedView = null;
}
mLastVisiblePos = 0;
}


private void firstFillCover(RecyclerView.Recycler recycler, RecyclerView.State state, int scrollDelta) {
if (mInitialSelectedPosition < 0 || mInitialSelectedPosition >= getItemCount()) {
fillCover(recycler, state, scrollDelta);
return;
}
if (mOrientation == HORIZONTAL) {
firstFillWithHorizontal(recycler, state);
} else {
firstFillWithVertical(recycler, state);
}

if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "firstFillCover finish:first: " + mFirstVisiblePosition + ",last:" + mLastVisiblePos);
}

Expand Down Expand Up @@ -210,6 +211,11 @@ private void firstFillWithHorizontal(RecyclerView.Recycler recycler, RecyclerVie
fillRight(recycler, mInitialSelectedPosition + 1, rightStartOffset, rightEdge);
}

@Override
public void onItemsRemoved(RecyclerView recyclerView, int positionStart, int itemCount) {
super.onItemsRemoved(recyclerView, positionStart, itemCount);
}

/**
* Layout the item view witch position special by {@link GalleryLayoutManager#mInitialSelectedPosition} first and then layout the other
*
Expand Down Expand Up @@ -407,7 +413,7 @@ private float calculateToCenterFraction(View child, float pendingOffset) {
int distance = calculateDistanceCenter(child, pendingOffset);
int childLength = mOrientation == GalleryLayoutManager.HORIZONTAL ? child.getWidth() : child.getHeight();

if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "calculateToCenterFraction: distance:" + distance + ",childLength:" + childLength);
}
return Math.max(-1.f, Math.min(1.f, distance * 1.f / childLength));
Expand Down Expand Up @@ -435,7 +441,7 @@ private int calculateDistanceCenter(View child, float pendingOffset) {
* @param dy
*/
private void fillWithVertical(RecyclerView.Recycler recycler, RecyclerView.State state, int dy) {
if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "fillWithVertical: dy:" + dy);
}
int topEdge = getOrientationHelper().getStartAfterPadding();
Expand All @@ -450,14 +456,14 @@ private void fillWithVertical(RecyclerView.Recycler recycler, RecyclerView.State
for (int i = 0; i < getChildCount(); i++) {
child = getChildAt(i + fixIndex);
if (getDecoratedBottom(child) - dy < topEdge) {
if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.v(TAG, "fillWithVertical: removeAndRecycleView:" + getPosition(child) + ",bottom:" + getDecoratedBottom(child));
}
removeAndRecycleView(child, recycler);
mFirstVisiblePosition++;
fixIndex--;
} else {
if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "fillWithVertical: break:" + getPosition(child) + ",bottom:" + getDecoratedBottom(child));
}
break;
Expand All @@ -468,7 +474,7 @@ private void fillWithVertical(RecyclerView.Recycler recycler, RecyclerView.State
for (int i = getChildCount() - 1; i >= 0; i--) {
child = getChildAt(i);
if (getDecoratedTop(child) - dy > bottomEdge) {
if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.v(TAG, "fillWithVertical: removeAndRecycleView:" + getPosition(child));
}
removeAndRecycleView(child, recycler);
Expand Down Expand Up @@ -516,7 +522,7 @@ private void fillWithVertical(RecyclerView.Recycler recycler, RecyclerView.State
layoutDecorated(scrap, scrapRect.left, scrapRect.top, scrapRect.right, scrapRect.bottom);
startOffset = scrapRect.bottom;
mLastVisiblePos = i;
if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "fillWithVertical: add view:" + i + ",startOffset:" + startOffset + ",mLastVisiblePos:" + mLastVisiblePos + ",bottomEdge" + bottomEdge);
}
}
Expand Down Expand Up @@ -555,7 +561,7 @@ private void fillWithVertical(RecyclerView.Recycler recycler, RecyclerView.State
private void fillWithHorizontal(RecyclerView.Recycler recycler, RecyclerView.State state, int dx) {
int leftEdge = getOrientationHelper().getStartAfterPadding();
int rightEdge = getOrientationHelper().getEndAfterPadding();
if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.v(TAG, "fillWithHorizontal() called with: dx = [" + dx + "],leftEdge:" + leftEdge + ",rightEdge:" + rightEdge);
}
//1.remove and recycle the view that disappear in screen
Expand All @@ -570,7 +576,7 @@ private void fillWithHorizontal(RecyclerView.Recycler recycler, RecyclerView.Sta
removeAndRecycleView(child, recycler);
mFirstVisiblePosition++;
fixIndex--;
if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.v(TAG, "fillWithHorizontal:removeAndRecycleView:" + getPosition(child) + " mFirstVisiblePosition change to:" + mFirstVisiblePosition);
}
} else {
Expand All @@ -584,7 +590,7 @@ private void fillWithHorizontal(RecyclerView.Recycler recycler, RecyclerView.Sta
if (getDecoratedLeft(child) - dx > rightEdge) {
removeAndRecycleView(child, recycler);
mLastVisiblePos--;
if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.v(TAG, "fillWithHorizontal:removeAndRecycleView:" + getPosition(child) + "mLastVisiblePos change to:" + mLastVisiblePos);
}
}
Expand All @@ -605,7 +611,7 @@ private void fillWithHorizontal(RecyclerView.Recycler recycler, RecyclerView.Sta
View lastView = getChildAt(getChildCount() - 1);
startPosition = getPosition(lastView) + 1; //start layout from next position item
startOffset = getDecoratedRight(lastView);
if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "fillWithHorizontal:to right startPosition:" + startPosition + ",startOffset:" + startOffset + ",rightEdge:" + rightEdge);
}
}
Expand All @@ -631,7 +637,7 @@ private void fillWithHorizontal(RecyclerView.Recycler recycler, RecyclerView.Sta
layoutDecorated(scrap, scrapRect.left, scrapRect.top, scrapRect.right, scrapRect.bottom);
startOffset = scrapRect.right;
mLastVisiblePos = i;
if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "fillWithHorizontal,layout:mLastVisiblePos: " + mLastVisiblePos);
}
}
Expand All @@ -641,7 +647,7 @@ private void fillWithHorizontal(RecyclerView.Recycler recycler, RecyclerView.Sta
View firstView = getChildAt(0);
startPosition = getPosition(firstView) - 1; //start layout from previous position item
startOffset = getDecoratedLeft(firstView);
if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "fillWithHorizontal:to left startPosition:" + startPosition + ",startOffset:" + startOffset + ",leftEdge:" + leftEdge + ",child count:" + getChildCount());
}
}
Expand Down Expand Up @@ -760,7 +766,7 @@ public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, Recycler
delta = -Math.min(0, Math.max(dx, ((child.getRight() - child.getLeft()) / 2 + child.getLeft()) - parentCenter));
}
}
if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "scrollHorizontallyBy: dx:" + dx + ",fixed:" + delta);
}
getState().mScrollDelta = -delta;
Expand Down Expand Up @@ -790,7 +796,7 @@ public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerVi
delta = -Math.min(0, Math.max(dy, (getDecoratedBottom(child) - getDecoratedTop(child)) / 2 + getDecoratedTop(child) - parentCenter));
}
}
if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "scrollVerticallyBy: dy:" + dy + ",fixed:" + delta);
}
getState().mScrollDelta = -delta;
Expand Down Expand Up @@ -899,7 +905,7 @@ public void attach(RecyclerView recyclerView, int selectedPosition) {
throw new IllegalArgumentException("The attach RecycleView must not null!!");
}
mRecyclerView = recyclerView;
mInitialSelectedPosition = selectedPosition;
mInitialSelectedPosition = Math.max(0, selectedPosition);
recyclerView.setLayoutManager(this);
mSnapHelper.attachToRecyclerView(recyclerView);
recyclerView.addOnScrollListener(mInnerScrollListener);
Expand Down Expand Up @@ -935,7 +941,7 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
mCurSelectedView.setSelected(true);
mCurSelectedPosition = selectedPosition;
if (!mCallbackInFling && mState != SCROLL_STATE_IDLE) {
if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.v(TAG, "ignore selection change callback when fling ");
}
mCallbackOnIdle = true;
Expand All @@ -946,7 +952,7 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
}
}
}
if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.v(TAG, "onScrolled: dx:" + dx + ",dy:" + dy);
}
}
Expand All @@ -955,7 +961,7 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
mState = newState;
if (DEBUG) {
if (BuildConfig.DEBUG) {
Log.v(TAG, "onScrollStateChanged: " + newState);
}
if (mState == SCROLL_STATE_IDLE) {
Expand Down

0 comments on commit 72b6e3a

Please sign in to comment.