Here are the examples of the java api class android.support.v7.widget.LinearSmoothScroller taken from open source projects.
1. SmoothLinearLayoutManager#smoothScrollToPosition()
Project: qksms
File: SmoothLinearLayoutManager.java
File: SmoothLinearLayoutManager.java
@Override public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, final int position) { super.smoothScrollToPosition(recyclerView, state, position); LinearSmoothScroller smoothScroller = new LinearSmoothScroller(mContext) { //This controls the direction in which smoothScroll looks for your view @Override public PointF computeScrollVectorForPosition(int targetPosition) { return new PointF(0, 1); } //This returns the milliseconds it takes to scroll one pixel. @Override protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) { return MILLISECONDS_PER_INCH / displayMetrics.densityDpi; } }; smoothScroller.setTargetPosition(position); startSmoothScroll(smoothScroller); }
2. HeaderLayoutManagerFixed#smoothScrollToPosition()
Project: android-parallax-recyclerview
File: HeaderLayoutManagerFixed.java
File: HeaderLayoutManagerFixed.java
@Override public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) { LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) { @Override public PointF computeScrollVectorForPosition(int targetPosition) { return HeaderLayoutManagerFixed.this.computeScrollVectorForPosition(targetPosition); } }; linearSmoothScroller.setTargetPosition(position); startSmoothScroll(linearSmoothScroller); }
3. TwoWayLayoutManager#smoothScrollToPosition()
Project: UltimateAndroid
File: TwoWayLayoutManager.java
File: TwoWayLayoutManager.java
@Override public void smoothScrollToPosition(RecyclerView recyclerView, State state, int position) { final LinearSmoothScroller scroller = new LinearSmoothScroller(recyclerView.getContext()) { @Override public PointF computeScrollVectorForPosition(int targetPosition) { if (getChildCount() == 0) { return null; } final int direction = targetPosition < getFirstVisiblePosition() ? -1 : 1; if (mIsVertical) { return new PointF(0, direction); } else { return new PointF(direction, 0); } } @Override protected int getVerticalSnapPreference() { return LinearSmoothScroller.SNAP_TO_START; } @Override protected int getHorizontalSnapPreference() { return LinearSmoothScroller.SNAP_TO_START; } }; scroller.setTargetPosition(position); startSmoothScroll(scroller); }
4. StaticGridLayoutManager#smoothScrollToPosition()
Project: UltimateAndroid
File: StaticGridLayoutManager.java
File: StaticGridLayoutManager.java
/* * You must override this method if you would like to support external calls * to animate a change to a new adapter position. The framework provides a * helper scroller implementation (LinearSmoothScroller), which we leverage * to do the animation calculations. */ @Override public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, final int position) { if (position >= getItemCount()) { Log.e(TAG, "Cannot scroll to " + position + ", item count is " + getItemCount()); return; } /* * LinearSmoothScroller's default behavior is to scroll the contents until * the child is fully visible. It will snap to the top-left or bottom-right * of the parent depending on whether the direction of travel was positive * or negative. */ LinearSmoothScroller scroller = new LinearSmoothScroller(recyclerView.getContext()) { /* * LinearSmoothScroller, at a minimum, just need to know the vector * (x/y distance) to travel in order to get from the current positioning * to the target. */ @Override public PointF computeScrollVectorForPosition(int targetPosition) { final int rowOffset = getGlobalRowOfPosition(targetPosition) - getGlobalRowOfPosition(mFirstVisiblePosition); final int columnOffset = getGlobalColumnOfPosition(targetPosition) - getGlobalColumnOfPosition(mFirstVisiblePosition); return new PointF(columnOffset * mDecoratedChildWidth, rowOffset * mDecoratedChildHeight); } }; scroller.setTargetPosition(position); startSmoothScroll(scroller); }
5. FixedGridLayoutManager#smoothScrollToPosition()
Project: UltimateAndroid
File: FixedGridLayoutManager.java
File: FixedGridLayoutManager.java
/* * You must override this method if you would like to support external calls * to animate a change to a new adapter position. The framework provides a * helper scroller implementation (LinearSmoothScroller), which we leverage * to do the animation calculations. */ @Override public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, final int position) { if (position >= getItemCount()) { Log.e(TAG, "Cannot scroll to " + position + ", item count is " + getItemCount()); return; } /* * LinearSmoothScroller's default behavior is to scroll the contents until * the child is fully visible. It will snap to the top-left or bottom-right * of the parent depending on whether the direction of travel was positive * or negative. */ LinearSmoothScroller scroller = new LinearSmoothScroller(recyclerView.getContext()) { /* * LinearSmoothScroller, at a minimum, just need to know the vector * (x/y distance) to travel in order to get from the current positioning * to the target. */ @Override public PointF computeScrollVectorForPosition(int targetPosition) { final int rowOffset = getGlobalRowOfPosition(targetPosition) - getGlobalRowOfPosition(mFirstVisiblePosition); final int columnOffset = getGlobalColumnOfPosition(targetPosition) - getGlobalColumnOfPosition(mFirstVisiblePosition); return new PointF(columnOffset * mDecoratedChildWidth, rowOffset * mDecoratedChildHeight); } }; scroller.setTargetPosition(position); startSmoothScroll(scroller); }
6. StaticGridLayoutManager#smoothScrollToPosition()
Project: UltimateAndroid
File: StaticGridLayoutManager.java
File: StaticGridLayoutManager.java
/* * You must override this method if you would like to support external calls * to animate a change to a new adapter position. The framework provides a * helper scroller implementation (LinearSmoothScroller), which we leverage * to do the animation calculations. */ @Override public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, final int position) { if (position >= getItemCount()) { Log.e(TAG, "Cannot scroll to " + position + ", item count is " + getItemCount()); return; } /* * LinearSmoothScroller's default behavior is to scroll the contents until * the child is fully visible. It will snap to the top-left or bottom-right * of the parent depending on whether the direction of travel was positive * or negative. */ LinearSmoothScroller scroller = new LinearSmoothScroller(recyclerView.getContext()) { /* * LinearSmoothScroller, at a minimum, just need to know the vector * (x/y distance) to travel in order to get from the current positioning * to the target. */ @Override public PointF computeScrollVectorForPosition(int targetPosition) { final int rowOffset = getGlobalRowOfPosition(targetPosition) - getGlobalRowOfPosition(mFirstVisiblePosition); final int columnOffset = getGlobalColumnOfPosition(targetPosition) - getGlobalColumnOfPosition(mFirstVisiblePosition); return new PointF(columnOffset * mDecoratedChildWidth, rowOffset * mDecoratedChildHeight); } }; scroller.setTargetPosition(position); startSmoothScroll(scroller); }
7. FixedGridLayoutManager#smoothScrollToPosition()
Project: UltimateAndroid
File: FixedGridLayoutManager.java
File: FixedGridLayoutManager.java
/* * You must override this method if you would like to support external calls * to animate a change to a new adapter position. The framework provides a * helper scroller implementation (LinearSmoothScroller), which we leverage * to do the animation calculations. */ @Override public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, final int position) { if (position >= getItemCount()) { Log.e(TAG, "Cannot scroll to " + position + ", item count is " + getItemCount()); return; } /* * LinearSmoothScroller's default behavior is to scroll the contents until * the child is fully visible. It will snap to the top-left or bottom-right * of the parent depending on whether the direction of travel was positive * or negative. */ LinearSmoothScroller scroller = new LinearSmoothScroller(recyclerView.getContext()) { /* * LinearSmoothScroller, at a minimum, just need to know the vector * (x/y distance) to travel in order to get from the current positioning * to the target. */ @Override public PointF computeScrollVectorForPosition(int targetPosition) { final int rowOffset = getGlobalRowOfPosition(targetPosition) - getGlobalRowOfPosition(mFirstVisiblePosition); final int columnOffset = getGlobalColumnOfPosition(targetPosition) - getGlobalColumnOfPosition(mFirstVisiblePosition); return new PointF(columnOffset * mDecoratedChildWidth, rowOffset * mDecoratedChildHeight); } }; scroller.setTargetPosition(position); startSmoothScroll(scroller); }
8. FixedGridLayoutManager#smoothScrollToPosition()
Project: recyclerview-playground
File: FixedGridLayoutManager.java
File: FixedGridLayoutManager.java
/* * You must override this method if you would like to support external calls * to animate a change to a new adapter position. The framework provides a * helper scroller implementation (LinearSmoothScroller), which we leverage * to do the animation calculations. */ @Override public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, final int position) { if (position >= getItemCount()) { Log.e(TAG, "Cannot scroll to " + position + ", item count is " + getItemCount()); return; } /* * LinearSmoothScroller's default behavior is to scroll the contents until * the child is fully visible. It will snap to the top-left or bottom-right * of the parent depending on whether the direction of travel was positive * or negative. */ LinearSmoothScroller scroller = new LinearSmoothScroller(recyclerView.getContext()) { /* * LinearSmoothScroller, at a minimum, just need to know the vector * (x/y distance) to travel in order to get from the current positioning * to the target. */ @Override public PointF computeScrollVectorForPosition(int targetPosition) { final int rowOffset = getGlobalRowOfPosition(targetPosition) - getGlobalRowOfPosition(mFirstVisiblePosition); final int columnOffset = getGlobalColumnOfPosition(targetPosition) - getGlobalColumnOfPosition(mFirstVisiblePosition); return new PointF(columnOffset * mDecoratedChildWidth, rowOffset * mDecoratedChildHeight); } }; scroller.setTargetPosition(position); startSmoothScroll(scroller); }
9. CleverLinearLayoutManager#initSmoothScroller()
Project: CleverRecyclerView
File: CleverLinearLayoutManager.java
File: CleverLinearLayoutManager.java
private void initSmoothScroller(Context context) { mSmoothScroller = new LinearSmoothScroller(context) { @Override public PointF computeScrollVectorForPosition(int targetPosition) { return CleverLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition); } @Override protected void onTargetFound(View targetView, RecyclerView.State state, Action action) { super.onTargetFound(targetView, state, action); //FIXME ???????... try { if (consecutiveUpdates == null) { consecutiveUpdates = Action.class.getDeclaredField("consecutiveUpdates"); //???????? consecutiveUpdates.setAccessible(true); } consecutiveUpdates.setInt(action, 0); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } @Override protected int calculateTimeForDeceleration(int dx) { return mDecelerationDuration; } @Override protected int getHorizontalSnapPreference() { return SNAP_TO_START; } @Override protected int getVerticalSnapPreference() { return SNAP_TO_START; } }; }