1

我正在尝试使平滑滚动条需要在 recyclerView 中的位置。我是 LinearLayoutManager 中的 ovverride 方法 smoothScrollToPosition,它工作正常。但是,我需要在屏幕顶部设置滚动位置(如果可能的话)。我也试过scrollToPositionWithOffset,它是我需要的,但现在没有平滑效果。如何混合使用这些方法,并在顶部设置项目进行平滑滚动?

private LinearLayoutManager linearLayoutManager = new    LinearLayoutManager(this){
    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView,
                                       RecyclerView.State state, final int position) {

        LinearSmoothScroller smoothScroller =
                new LinearSmoothScroller(getApplicationContext()) {

                    //This controls the direction in which smoothScroll looks
                    //for your view
                    @Override
                    public PointF computeScrollVectorForPosition
                    (int targetPosition) {
                        return this
                                .computeScrollVectorForPosition(targetPosition);
                    }

                    //This returns the milliseconds it takes to
                    //scroll one pixel.
                    @Override
                    protected float calculateSpeedPerPixel
                    (DisplayMetrics displayMetrics) {
                        return 50f/displayMetrics.densityDpi;
                    }
                };

        smoothScroller.setTargetPosition(position);
        startSmoothScroll(smoothScroller);
    }
};
4

1 回答 1

4

这是迟到的回应,但我想建议一个库SnappySmoothScroller。该库支持使用特定的捕捉位置平滑滚动:SnapType.STARTSnapType.CENTERSnapType.END

像这样使用:

    private LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this) {
        @Override
        public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
            SnappySmoothScroller scroller = new SnappySmoothScroller.Builder()
                    .setPosition(position)
                    .setSnapType(SnapType.START)
                    .setScrollVectorDetector(new LinearLayoutScrollVectorDetector(this))
                    .build(recyclerView.getContext());

            startSmoothScroll(scroller);
        }
    };
于 2016-10-21T10:26:04.600 回答