提问者:小点点

循环视图平滑滚动


我正在尝试使平滑的滚动需要在回收视图中的位置。我在LinearLayoutManager中使用ovverides方法平滑ScrollToPoplace,它的工作很好。但是,我需要在屏幕顶部设置滚动位置(如果可能的话)。我也尝试过scrollToPositionRetOffset,它是我需要的,但是现在,没有平滑的效果。如何混合这些方法,并在顶部设置项目进行平滑滚动?

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);
    }
};

共1个答案

匿名用户

这是一个延迟响应,但我想建议一个库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);
        }
    };