美文网首页RecyclerView
RecyclerView滚动速度可控

RecyclerView滚动速度可控

作者: 陈萍儿Candy | 来源:发表于2020-07-15 15:53 被阅读0次

重写recylerView对应的layoutManager的smoothScrollToPosition方法,定义滚动速度:

@Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        if (linearSmoothScroller == null) {
            linearSmoothScroller =
                    new LinearSmoothScroller(recyclerView.getContext()) {

                        /**
                         * 计算滚动的速度
                         *
                         * @param displayMetrics 屏幕分辨率
                         * @return 1px滑过的时间(ms)
                         */
                        @Override
                        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                            //densityDpi 360dpi/480dpi...
                            return 200F / (float) displayMetrics.densityDpi;
                        }

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

不管是GridLayoutManager还是LinearLayoutManager,都管用

相关文章

网友评论

    本文标题:RecyclerView滚动速度可控

    本文链接:https://www.haomeiwen.com/subject/tsnphktx.html