Skip to content

Commit

Permalink
Added feature to allow sliding of multiple views on fling gesture. us…
Browse files Browse the repository at this point in the history
…e 'picker.setSlideOnFling(true)' to use it. Custom threshold set to 2100 which can dynamically be set by user using 'picker.setSlideOnFlingThreshold(value)'.
  • Loading branch information
srujanb committed Jun 21, 2017
1 parent 0075182 commit 34b23fc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DiscreteScrollLayoutManager extends RecyclerView.LayoutManager {
static final int NO_POSITION = -1;

private static final String EXTRA_POSITION = "extra_position";
private static final int DEFAULT_TIME_FOR_ITEM_SETTLE = 150;
private static final int DEFAULT_TIME_FOR_ITEM_SETTLE = 300;

//This field will take value of all visible view's center points during the fill phase
private Point viewCenterIterator;
Expand Down Expand Up @@ -57,6 +57,9 @@ class DiscreteScrollLayoutManager extends RecyclerView.LayoutManager {
private boolean dataSetChangeShiftedPosition;
private boolean isFirstOrEmptyLayout;

private int flingThreshold = 2100; //Decrease to increase sensitivity.
private boolean shouldSlideOnFling = false;

@NonNull
private final ScrollStateListener scrollStateListener;
private DiscreteScrollItemTransformer itemTransformer;
Expand Down Expand Up @@ -183,6 +186,7 @@ private void layoutViews(RecyclerView.Recycler recycler, Direction direction, in
}

private void layoutView(RecyclerView.Recycler recycler, int position, Point viewCenter) {
if (position < 0) return;
View v = detachedCache.get(position);
if (v == null) {
v = recycler.getViewForPosition(position);
Expand Down Expand Up @@ -414,19 +418,32 @@ private void onDragStart() {

public void onFling(int velocityX, int velocityY) {
int velocity = orientationHelper.getFlingVelocity(velocityX, velocityY);
int newPosition = currentPosition + Direction.fromDelta(velocity).applyTo(1);
int throttleValue = shouldSlideOnFling ? Math.abs(velocityX/flingThreshold) : 1;
int newPosition = currentPosition + Direction.fromDelta(velocity).applyTo(throttleValue);
if (currentPosition != 0 && newPosition < 0)
newPosition = 0;
else if (currentPosition != getItemCount() -1 && newPosition >= getItemCount())
newPosition = getItemCount() - 1;
boolean isInScrollDirection = velocity * scrolled >= 0;
boolean canFling = isInScrollDirection && newPosition >= 0 && newPosition < getItemCount();
if (canFling) {
pendingScroll = getHowMuchIsLeftToScroll(velocity);
if (pendingScroll != 0) {
startSmoothPendingScroll();
startSmoothPendingScroll(newPosition);
}
} else {
returnToCurrentPosition();
}
}

public void setShouldSlideOnFling(Boolean result){
this.shouldSlideOnFling = result;
}

public void setSlideOnFlingThreshold(int threshold){
this.flingThreshold = threshold;
}

public void returnToCurrentPosition() {
pendingScroll = -scrolled;
if (pendingScroll != 0) {
Expand Down Expand Up @@ -465,6 +482,16 @@ private void startSmoothPendingScroll() {
startSmoothScroll(scroller);
}

private void startSmoothPendingScroll(int position){
if (currentPosition == position) return;
pendingScroll = -scrolled;
Direction direction = Direction.fromDelta(position - currentPosition);
int distanceToScroll = Math.abs(position - currentPosition) * scrollToChangeCurrent;
pendingScroll += direction.applyTo(distanceToScroll);
pendingPosition = position;
startSmoothPendingScroll();
}

@Override
public void onAdapterChanged(RecyclerView.Adapter oldAdapter, RecyclerView.Adapter newAdapter) {
if (newAdapter.getItemCount() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ public void setItemTransitionTimeMillis(@IntRange(from = 10) int millis) {
layoutManager.setTimeForItemSettle(millis);
}

public void setSlideOnFling(Boolean result){
layoutManager.setShouldSlideOnFling(result);
}

public void setSlideOnFlingThreshold(int threshold){
layoutManager.setSlideOnFlingThreshold(threshold);
}

public void setOrientation(Orientation orientation) {
layoutManager.setOrientation(orientation);
}
Expand Down

0 comments on commit 34b23fc

Please sign in to comment.