类android.support.v7.widget.LinearSmoothScroller源码实例Demo

下面列出了怎么用android.support.v7.widget.LinearSmoothScroller的API类实例代码及写法,或者点击链接到github查看源代码。

@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
    LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()){
        @Nullable
        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            return ScrollSpeedLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);
        }

        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            setSpeedSlow();
            return MILLISECONDS_PER_INCH / displayMetrics.density;
        }
    };

    linearSmoothScroller.setTargetPosition(position);
    startSmoothScroll(linearSmoothScroller);

}
 
源代码2 项目: imsdk-android   文件: WorkWorldFragment.java
@Override
    public void scrollTop() {
        if (work_world_rc != null) {
            if(workWorldAdapter.getItemCount()>5){
                work_world_rc.scrollToPosition(5);
            }
            if(getActivity()!=null){
                LinearSmoothScroller smoothScroller = new WorkWorldDetailsActivity.TopSmoothScroller(getActivity());
                smoothScroller.setTargetPosition(0);
                mRcManager.startSmoothScroll(smoothScroller);
            }else{
                work_world_rc.smoothScrollToPosition(0);
            }

//
        }

    }
 
@Override
public void smoothScrollToPosition(final RecyclerView recyclerView, RecyclerView.State state, int position) {
   final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
      @Override
      public int calculateDyToMakeVisible(View view, int snapPreference) {
         final RecyclerView.LayoutManager layoutManager = getLayoutManager();
         if (layoutManager == null || !layoutManager.canScrollVertically()) {
            return 0;
         }

         final int adapterPosition = getPosition(view);
         final int topOffset = getPositionSectionHeaderHeight(adapterPosition);
         final int top = layoutManager.getDecoratedTop(view);
         final int bottom = layoutManager.getDecoratedBottom(view);
         final int start = layoutManager.getPaddingTop() + topOffset;
         final int end = layoutManager.getHeight() - layoutManager.getPaddingBottom();
         return calculateDtToFit(top, bottom, start, end, snapPreference);
      }
   };
   linearSmoothScroller.setTargetPosition(position);
   startSmoothScroll(linearSmoothScroller);
}
 
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, final int position) {
    LinearSmoothScroller smoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }

        @Nullable
        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            return WheelPickerLayoutManager.this.computeScrollVectorForPosition(targetPosition);
        }
    };

    smoothScroller.setTargetPosition(position);
    startSmoothScroll(smoothScroller);
}
 
源代码5 项目: ToDay   文件: ToDayView.java
@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 AgendaLinearLayoutManager.this
                            .computeScrollVectorForPosition(targetPosition);
                }

                @Override
                protected int getVerticalSnapPreference() {
                    return SNAP_TO_START; // override base class behavior
                }
            };
    smoothScroller.setTargetPosition(position);
    startSmoothScroll(smoothScroller);
}
 
源代码6 项目: GridPagerSnapHelper   文件: SnapHelper.java
/**
 * Creates a scroller to be used in the snapping implementation.
 *
 * @param layoutManager The {@link RecyclerView.LayoutManager} associated with the attached
 *                      {@link RecyclerView}.
 * @return a {@link LinearSmoothScroller} which will handle the scrolling.
 */
@Nullable
protected LinearSmoothScroller createSnapScroller(RecyclerView.LayoutManager layoutManager) {
    if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
        return null;
    }
    return new LinearSmoothScroller(mRecyclerView.getContext()) {
        @Override
        protected void onTargetFound(View targetView, RecyclerView.State state, Action action) {
            int[] snapDistances = calculateDistanceToFinalSnap(mRecyclerView.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 MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }
    };
}
 
源代码7 项目: iGap-Android   文件: PreCachingLayoutManager.java
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {

    final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {

        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            return PreCachingLayoutManager.this.computeScrollVectorForPosition(targetPosition);
        }

        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }
    };

    linearSmoothScroller.setTargetPosition(position);
    startSmoothScroll(linearSmoothScroller);
}
 
源代码8 项目: diycode   文件: SpeedyLinearLayoutManager.java
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int
        position) {

    final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView
            .getContext()) {

        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            return SpeedyLinearLayoutManager.this.computeScrollVectorForPosition
                    (targetPosition);
        }

        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }
    };

    linearSmoothScroller.setTargetPosition(position);
    startSmoothScroll(linearSmoothScroller);
}
 
源代码9 项目: TvRecyclerView   文件: GridLayoutManager.java
private int startPositionSmoothScroller(int position) {
    LinearSmoothScroller linearSmoothScroller = new GridLinearSmoothScroller() {
        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            if (getChildCount() == 0) {
                return null;
            }
            final int firstChildPos = getPosition(getChildAt(0));
            final boolean isStart = targetPosition < firstChildPos;
            final int direction = isStart ? -1 : 1;
            if (mOrientation == HORIZONTAL) {
                return new PointF(direction, 0);
            } else {
                return new PointF(0, direction);
            }
        }

        @Override
        protected void onStop() {
            super.onStop();
        }
    };
    linearSmoothScroller.setTargetPosition(position);
    startSmoothScroll(linearSmoothScroller);
    return linearSmoothScroller.getTargetPosition();
}
 
源代码10 项目: xDrip   文件: ActivityWithRecycler.java
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {

    final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {

        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            return CustomLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);
        }

        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }
    };

    linearSmoothScroller.setTargetPosition(position);
    try {
        startSmoothScroll(linearSmoothScroller);
    } catch (IllegalArgumentException e) {
        // couldn't scroll for some reason, just ignore
    }
}
 
源代码11 项目: xDrip-plus   文件: ActivityWithRecycler.java
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {

    final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {

        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            return CustomLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);
        }

        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }
    };

    linearSmoothScroller.setTargetPosition(position);
    try {
        startSmoothScroll(linearSmoothScroller);
    } catch (IllegalArgumentException e) {
        // couldn't scroll for some reason, just ignore
    }
}
 
源代码12 项目: Ouroboros   文件: SnappyLinearLayoutManager.java
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
    final LinearSmoothScroller linearSmoothScroller =
            new LinearSmoothScroller(recyclerView.getContext()) {

                // I want a behavior where the scrolling always snaps to the beginning of
                // the list. Snapping to end is also trivial given the default implementation.
                // If you need a different behavior, you may need to override more
                // of the LinearSmoothScrolling methods.
                protected int getHorizontalSnapPreference() {
                    return SNAP_TO_START;
                }

                protected int getVerticalSnapPreference() {
                    return SNAP_TO_START;
                }

                @Override
                public PointF computeScrollVectorForPosition(int targetPosition) {
                    return SnappyLinearLayoutManager.this
                            .computeScrollVectorForPosition(targetPosition);
                }
            };
    linearSmoothScroller.setTargetPosition(position);
    startSmoothScroll(linearSmoothScroller);
}
 
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
    LinearSmoothScroller smoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {

        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return smoothScrollSpeed / displayMetrics.densityDpi;
        }

    };
    smoothScroller.setTargetPosition(position);
    startSmoothScroll(smoothScroller);
}
 
/**
 * 平滑滚动到指定页面
 *
 * @param pageIndex 页面下标
 */
public void smoothScrollToPage(int pageIndex) {
    if (pageIndex < 0 || pageIndex >= mLastPageCount) {
        Log.e(TAG, "pageIndex is outOfIndex, must in [0, " + mLastPageCount + ").");
        return;
    }
    if (null == mRecyclerView) {
        Log.e(TAG, "RecyclerView Not Found!");
        return;
    }

    // 如果滚动到页面之间距离过大,先直接滚动到目标页面到临近页面,在使用 smoothScroll 最终滚动到目标
    // 否则在滚动距离很大时,会导致滚动耗费的时间非常长
    int currentPageIndex = getPageIndexByOffset();
    if (Math.abs(pageIndex - currentPageIndex) > 3) {
        if (pageIndex > currentPageIndex) {
            scrollToPage(pageIndex - 3);
        } else if (pageIndex < currentPageIndex) {
            scrollToPage(pageIndex + 3);
        }
    }

    // 具体执行滚动
    LinearSmoothScroller smoothScroller = new PagerGridSmoothScroller(mRecyclerView);
    int position = pageIndex * mOnePageSize;
    smoothScroller.setTargetPosition(position);
    startSmoothScroll(smoothScroller);
}
 
源代码15 项目: pager-layoutmanager   文件: PagerGridSnapHelper.java
/**
 * 通过自定义 LinearSmoothScroller 来控制速度
 *
 * @param layoutManager 布局故哪里去
 * @return 自定义 LinearSmoothScroller
 */
protected LinearSmoothScroller createSnapScroller(RecyclerView.LayoutManager layoutManager) {
    if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
        return null;
    }
    return new PagerGridSmoothScroller(mRecyclerView);
}
 
源代码16 项目: YCBanner   文件: ScrollSnapHelper.java
@Nullable
protected LinearSmoothScroller createSnapScroller(final RecyclerView.LayoutManager layoutManager) {
    if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
        return null;
    }
    return new LinearSmoothScroller(mRecyclerView.getContext()) {
        @Override
        protected void onTargetFound(View targetView, RecyclerView.State state, Action action) {
            int[] snapDistances = calculateDistanceToFinalSnap(mRecyclerView.getLayoutManager(), targetView);
            final int dx;
            final int dy;
            if (snapDistances != null) {
                dx = snapDistances[0];
                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 MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }
    };
}
 
源代码17 项目: YCBanner   文件: ScrollLinearHelper.java
@Nullable
protected LinearSmoothScroller createSnapScroller(final RecyclerView.LayoutManager layoutManager) {
    if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
        return null;
    }
    return new LinearSmoothScroller(mRecyclerView.getContext()) {
        @Override
        protected void onTargetFound(View targetView, RecyclerView.State state, RecyclerView.SmoothScroller.Action action) {
            int[] snapDistances = calculateDistanceToFinalSnap(mRecyclerView.getLayoutManager(), targetView);
            final int dx;
            final int dy;
            if (snapDistances != null) {
                dx = snapDistances[0];
                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 MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }
    };
}
 
@Override
public void smoothScrollToPosition(
        RecyclerView recyclerView, RecyclerView.State state, int position) {
    if (position >= getItemCount()) position = getItemCount() - 1;

    LinearSmoothScroller scroller = new LinearSmoothScroller(recyclerView.getContext()) {
        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            final int rowOffset = getRowIndex(targetPosition) - firstVisibleRow;
            return new PointF(0, rowOffset * cellHeight);
        }
    };
    scroller.setTargetPosition(position);
    startSmoothScroll(scroller);
}
 
@Override
public RecyclerView.SmoothScroller createSmoothScroller(@NonNull Context context, final int position, final int timeMs, final AnchorViewState anchor) {
    return new LinearSmoothScroller(context) {
        /*
         * 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) {
            int visiblePosition = anchor.getPosition();
            //determine scroll up or scroll down needed
            return new PointF(position > visiblePosition ? 1 : -1, 0);
        }

        @Override
        protected void onTargetFound(View targetView, RecyclerView.State state, Action action) {
            super.onTargetFound(targetView, state, action);
            int currentLeft = layoutManager.getPaddingLeft();
            int desiredLeft = layoutManager.getDecoratedLeft(targetView);

            int dx = desiredLeft - currentLeft;

            //perform fit animation to move target view at top of layoutX
            action.update(dx, 0, timeMs, new LinearInterpolator());
        }
    };
}
 
@Override
public RecyclerView.SmoothScroller createSmoothScroller(@NonNull Context context, final int position, final int timeMs, final AnchorViewState anchor) {
    return new LinearSmoothScroller(context) {
        /*
         * 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) {
            int visiblePosition = anchor.getPosition();
            //determine scroll up or scroll down needed
            return new PointF(0, position > visiblePosition ? 1 : -1);
        }

        @Override
        protected void onTargetFound(View targetView, RecyclerView.State state, Action action) {
            super.onTargetFound(targetView, state, action);
            int desiredTop = lm.getPaddingTop();
            int currentTop = lm.getDecoratedTop(targetView);

            int dy = currentTop - desiredTop;

            //perform fit animation to move target view at top of layout
            action.update(0, dy, timeMs, new LinearInterpolator());
        }
    };
}
 
@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
    LinearSmoothScroller scroller = new LinearSmoothScroller(_context) {
        @Override
        public PointF computeScrollVectorForPosition(int targetPosition) {
            return ScrollableLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);
        }
    };
    scroller.setTargetPosition(position);
    startSmoothScroll(scroller);

}
 
源代码22 项目: SuntimesWidget   文件: GravitySnapHelper.java
@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;
        }
    };
}
 
源代码23 项目: AndroidLibs   文件: FlowLayoutManager.java
@Override
public void smoothScrollToPosition(final RecyclerView recyclerView, final RecyclerView.State state, int position) {
	RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
		@Override
		public PointF computeScrollVectorForPosition(int targetPosition) {
			return new PointF(0, getOffsetOfItemToFirstChild(targetPosition, recyclerRef));
		}
	};
	smoothScroller.setTargetPosition(position);
	startSmoothScroll(smoothScroller);
}
 
@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);
}
 
void setShowVersions(boolean showVersions, boolean scrollTo) {
    this.showVersions = showVersions;
    boolean itemsWereRemoved = items.removeAll(versions);
    int startIndex = items.indexOf(VIEWTYPE_VERSIONS) + 1;

    // When adding/removing items, be sure to only notifyItemInserted and notifyItemRemoved
    // rather than notifyDatasetChanged(). If we only notify about the entire thing, then
    // everything gets rebuilt, including the expandable "Versions" item. By rebuilding that
    // item it will interrupt the nice material-design-style-ripple from the background.
    if (showVersions) {
        items.addAll(startIndex, versions);
        notifyItemRangeInserted(startIndex, versions.size());
        if (recyclerView != null && scrollTo) {
            final LinearSmoothScroller smoothScroller = new LinearSmoothScroller(context) {
                @Override
                protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                    // The default speed of smooth scrolling doesn't look good
                    // and it's too fast when it happens while inserting
                    // multiple recycler view items
                    return 75f / displayMetrics.densityDpi;
                }
            };
            // Expanding the version list reveals up to 5 items by default
            int visibleVersionLimit = Math.min(versions.size(), 5);
            smoothScroller.setTargetPosition(startIndex + visibleVersionLimit - 1);
            recyclerView.getLayoutManager().startSmoothScroll(smoothScroller);
        }
    } else if (itemsWereRemoved) {
        notifyItemRangeRemoved(startIndex, versions.size());
        if (recyclerView != null && scrollTo) {
            recyclerView.smoothScrollToPosition(startIndex - 1);
        }
    }
}
 
@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);
}
 
源代码27 项目: UltimateAndroid   文件: 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);
}
 
源代码28 项目: UltimateAndroid   文件: StaticGridLayoutManager.java
@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);
}
 
源代码29 项目: UltimateAndroid   文件: FixedGridLayoutManager.java
@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);
}
 
源代码30 项目: UltimateAndroid   文件: StaticGridLayoutManager.java
@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);
}
 
 同包方法