下面列出了androidx.recyclerview.widget.RecyclerView#SmoothScroller ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
}
@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);
}
/**
* @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;
}
@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);
}
@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);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
int position) {
RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
/**
* @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);
}
}
/**
* @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);
}
@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;
}
};
}
@Override public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
int position) {
RecyclerView.SmoothScroller smoothScroller =
new TopSnappedSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
private void smoothScrollToNowPlaying() {
updateSpacers();
RecyclerView.SmoothScroller scroller =
new SnappingScroller(getContext(), SnappingScroller.SNAP_TO_START);
scroller.setTargetPosition(lastPlayIndex);
mRecyclerView.getLayoutManager().startSmoothScroll(scroller);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
int position) {
RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
RecyclerView.SmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext());
smoothScroller.setTargetPosition(position);
startSmoothScroll(smoothScroller);
}
/**
* 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);
}