Java Code Examples for androidx.recyclerview.widget.RecyclerView#SmoothScroller

The following examples show how to use androidx.recyclerview.widget.RecyclerView#SmoothScroller . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContactRecyclerViewAdapter.java    From BaldPhone with Apache License 2.0 6 votes vote down vote up
public void changeCursor(@NonNull Cursor cursor) {
    this.cursor = cursor;
    applyToCursor();
    notifyDataSetChanged();

    if (getItemCount() > 0) {
        RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(activity) {
            @Override
            protected int getVerticalSnapPreference() {
                return LinearSmoothScroller.SNAP_TO_START;
            }
        };
        smoothScroller.setTargetPosition(0);
        recyclerView.getLayoutManager().startSmoothScroll(smoothScroller);
    }

}
 
Example 2
Source File: RecyclerBinder.java    From litho with Apache License 2.0 6 votes vote down vote up
@UiThread
public void scrollSmoothToPosition(
    int position, final int offset, final SmoothScrollAlignmentType type) {
  if (mMountedView == null) {
    mCurrentFirstVisiblePosition = position;
    mCurrentOffset = offset;
    mSmoothScrollAlignmentType = type;
    return;
  }

  final int target = type == SmoothScrollAlignmentType.SNAP_TO_CENTER ? position + 1 : position;

  final RecyclerView.SmoothScroller smoothScroller =
      SnapUtil.getSmoothScrollerWithOffset(mComponentContext.getAndroidContext(), offset, type);
  smoothScroller.setTargetPosition(target);
  mMountedView.getLayoutManager().startSmoothScroll(smoothScroller);
}
 
Example 3
Source File: GravitySnapHelper.java    From GravitySnapHelper with Apache License 2.0 6 votes vote down vote up
/**
 * @return true if the scroll will snap to a view, false otherwise
 */
private boolean scrollTo(int position, boolean smooth) {
    if (recyclerView.getLayoutManager() != null) {
        if (smooth) {
            RecyclerView.SmoothScroller smoothScroller
                    = createScroller(recyclerView.getLayoutManager());
            if (smoothScroller != null) {
                smoothScroller.setTargetPosition(position);
                recyclerView.getLayoutManager().startSmoothScroll(smoothScroller);
                return true;
            }
        } else {
            RecyclerView.ViewHolder viewHolder
                    = recyclerView.findViewHolderForAdapterPosition(position);
            if (viewHolder != null) {
                int[] distances = calculateDistanceToFinalSnap(recyclerView.getLayoutManager(),
                        viewHolder.itemView);
                recyclerView.scrollBy(distances[0], distances[1]);
                return true;
            }
        }
    }
    return false;
}
 
Example 4
Source File: SnappyLinearLayoutManager.java    From materialistic with Apache License 2.0 6 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView,
                                   RecyclerView.State state,
                                   int position) {
    RecyclerView.SmoothScroller smoothScroller =
            new LinearSmoothScroller(recyclerView.getContext()) {
                @Override
                public PointF computeScrollVectorForPosition(int targetPosition) {
                    return SnappyLinearLayoutManager.this
                            .computeScrollVectorForPosition(targetPosition);
                }

                @Override
                protected int getVerticalSnapPreference() {
                    return SNAP_TO_START; // override base class behavior
                }
            };
    smoothScroller.setTargetPosition(position);
    startSmoothScroll(smoothScroller);
}
 
Example 5
Source File: FindBookFragment.java    From a with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
    Log.e("linksu",
            "smoothScrollToPosition(ScrollSpeedLinearLayoutManger.java:62)");
    RecyclerView.SmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
    smoothScroller.setTargetPosition(position);
    startSmoothScroll(smoothScroller);
}
 
Example 6
Source File: MainActivityPresenter.java    From shinny-futures-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
                                   int position) {
    RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext());
    smoothScroller.setTargetPosition(position);
    startSmoothScroll(smoothScroller);
}
 
Example 7
Source File: SnapUtil.java    From litho with Apache License 2.0 5 votes vote down vote up
/**
 * @return {@link androidx.recyclerview.widget.RecyclerView.SmoothScroller} that takes snapping
 *     into account.
 */
public static RecyclerView.SmoothScroller getSmoothScrollerWithOffset(
    Context context, final int offset, final SmoothScrollAlignmentType type) {
  if (type == SmoothScrollAlignmentType.SNAP_TO_ANY
      || type == SmoothScrollAlignmentType.SNAP_TO_START
      || type == SmoothScrollAlignmentType.SNAP_TO_END) {
    final int snapPreference = type.getValue();
    return new EdgeSnappingSmoothScroller(context, snapPreference, offset);
  } else if (type == SmoothScrollAlignmentType.SNAP_TO_CENTER) {
    return new CenterSnappingSmoothScroller(context, offset);
  } else {
    return new LinearSmoothScroller(context);
  }
}
 
Example 8
Source File: RecyclerCollectionEventsController.java    From litho with Apache License 2.0 5 votes vote down vote up
/**
 * @param animated whether the scroll will happen with animation.
 * @param defaultTarget target to use as fallback.
 * @param snapTarget target that takes into account snapping behavior.
 * @param smoothScroller custom smooth scroller
 */
private void requestScrollToPositionInner(
    boolean animated,
    int defaultTarget,
    int snapTarget,
    RecyclerView.SmoothScroller smoothScroller) {
  final RecyclerView recyclerView = getRecyclerView();
  if (recyclerView == null) {
    return;
  }

  final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
  if (layoutManager == null || recyclerView.isLayoutFrozen()) {
    return;
  }

  if (!animated) {
    requestScrollToPosition(defaultTarget, false);
    return;
  }

  if (smoothScroller == null && mSnapMode == SNAP_NONE) {
    requestScrollToPosition(defaultTarget, true);
    return;
  }

  if (smoothScroller == null) {
    smoothScroller =
        SnapUtil.getSmoothScrollerWithOffset(
            recyclerView.getContext(), 0, getSmoothScrollAlignmentTypeFrom(mSnapMode));
  }

  smoothScroller.setTargetPosition(snapTarget);
  layoutManager.startSmoothScroll(smoothScroller);
}
 
Example 9
Source File: GravitySnapHelper.java    From GravitySnapHelper with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public RecyclerView.SmoothScroller createScroller(RecyclerView.LayoutManager layoutManager) {
    if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)
            || recyclerView == null) {
        return null;
    }
    return new LinearSmoothScroller(recyclerView.getContext()) {
        @Override
        protected void onTargetFound(View targetView,
                                     RecyclerView.State state,
                                     RecyclerView.SmoothScroller.Action action) {
            if (recyclerView == null || recyclerView.getLayoutManager() == null) {
                // The associated RecyclerView has been removed so there is no action to take.
                return;
            }
            int[] snapDistances = calculateDistanceToFinalSnap(recyclerView.getLayoutManager(),
                    targetView);
            final int dx = snapDistances[0];
            final int dy = snapDistances[1];
            final int time = calculateTimeForDeceleration(Math.max(Math.abs(dx), Math.abs(dy)));
            if (time > 0) {
                action.update(dx, dy, time, mDecelerateInterpolator);
            }
        }

        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return scrollMsPerInch / displayMetrics.densityDpi;
        }
    };
}
 
Example 10
Source File: LinearLayoutManagerWithSmoothScroller.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
@Override public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
    int position) {
  RecyclerView.SmoothScroller smoothScroller =
      new TopSnappedSmoothScroller(recyclerView.getContext());
  smoothScroller.setTargetPosition(position);
  startSmoothScroll(smoothScroller);
}
 
Example 11
Source File: QueueFragment.java    From Jockey with Apache License 2.0 5 votes vote down vote up
private void smoothScrollToNowPlaying() {
    updateSpacers();

    RecyclerView.SmoothScroller scroller =
            new SnappingScroller(getContext(), SnappingScroller.SNAP_TO_START);
    scroller.setTargetPosition(lastPlayIndex);
    mRecyclerView.getLayoutManager().startSmoothScroll(scroller);
}
 
Example 12
Source File: SmoothLinearLayoutManager.java    From hipda with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
                                   int position) {
    RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext());
    smoothScroller.setTargetPosition(position);
    startSmoothScroll(smoothScroller);
}
 
Example 13
Source File: FindBookFragment.java    From MyBookshelf with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
    RecyclerView.SmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
    smoothScroller.setTargetPosition(position);
    startSmoothScroll(smoothScroller);
}
 
Example 14
Source File: SnappingLinearLayoutManager.java    From GetApk with MIT License 4 votes vote down vote up
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
    RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext());
    smoothScroller.setTargetPosition(position);
    startSmoothScroll(smoothScroller);
}
 
Example 15
Source File: RecyclerCollectionEventsController.java    From litho with Apache License 2.0 2 votes vote down vote up
/**
 * Send the {@link RecyclerCollectionComponent} a request to scroll the content to the given
 * target position taking into account provided snapping behavior. The provided smoothScroller is
 * used to scroll to the target.
 */
public void requestScrollToPositionWithSnap(
    final int target, final RecyclerView.SmoothScroller smoothScroller) {
  requestScrollToPositionInner(true, target, target, smoothScroller);
}