【问题标题】:Horizontal Recycler view auto scrolling error水平回收器视图自动滚动错误
【发布时间】:2017-02-20 08:47:04
【问题描述】:

我有这个RecyclerView 用作自动收报机,起初自动滚动工作正常,但一段时间后它变得奇怪(来回)并且RecyclerView 卡在一个项目上而不再平滑滚动,谁能帮帮我。

这是我的布局管理器:

LinearLayoutManager layoutManager = new LinearLayoutManager(HomeActivity.this, LinearLayoutManager.HORIZONTAL, false) {
        @Override
        public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
            LinearSmoothScroller smoothScroller = new  LinearSmoothScroller(HomeActivity.this) {
                private static final float SPEED = 5500f;// Change this value (default=25f)
                @Override
                protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                    return SPEED / displayMetrics.densityDpi;
                }
            };
            smoothScroller.setTargetPosition(position);
            startSmoothScroll(smoothScroller);
        }

    };
    rvTicker.setLayoutManager(layoutManager);

这是我的自动滚动功能:

public void autoScroll(){
    speedScroll = 0;
    handler = new Handler();
    runnable = new Runnable() {
        int count = 0;
        @Override
        public void run() {
            if(count == tickerAdapter.getItemCount())
                count = 0;
            if(count < tickerAdapter.getItemCount()){
                rvTicker.smoothScrollToPosition(++count);
                handler.postDelayed(this,speedScroll);
            }
        }
    };
    handler.postDelayed(runnable,speedScroll);
}

【问题讨论】:

    标签: android android-recyclerview smooth-scrolling


    【解决方案1】:

    已解决,我调整了 autoscroll 函数:

    public void autoScroll(){
        speedScroll = 0;
        handler = new Handler();
        runnable = new Runnable() {
            int count = 0;
            @Override
            public void run() {
                if(count == tickerAdapter.getItemCount())
                    count = 0;
                else {
                    if(count < tickerAdapter.getItemCount()){
                        rvTicker.smoothScrollToPosition(++count);
                        handler.postDelayed(this,speedScroll);
                    }else {
                        count = 0;
                    }
                }
                Log.wtf("tickerAdapter.getItemCount()", tickerAdapter.getItemCount()+"");
                Log.wtf("count", count+"");
                Log.wtf("++count", ++count+"");
                Log.wtf("==============","=============");
            }
        };
        handler.postDelayed(runnable,speedScroll);
    }
    

    【讨论】:

    • 嗨,自动滚动的完美解决方案。谢谢。但是当单击或滚动手动停止自动滚动时。有没有办法在用户点击/滚动时维护它?
    猜你喜欢
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 1970-01-01
    • 2021-06-08
    相关资源
    最近更新 更多