android - Horizo​​ntal Recycler View 自动滚动错误

标签 android android-recyclerview smooth-scrolling

我有一个用作自动收报机的 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);
}

最佳答案

已解决,我调整了 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);
}

关于android - Horizo​​ntal Recycler View 自动滚动错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42339935/

相关文章:

android - Android 是否在 "Shared-Cache Mode"中使用 SQLITE?

android - 从适配器 RecyclerView 打开 fragment

android - RecyclerView ViewHolders 中的许多 CountDownTimer 出错

javascript - jquery-smooth-scroll-rails 不工作

javascript - anchor 滚动功能干扰滚动事件监听器逻辑以隐藏/显示 "back to top"按钮

javascript - 将垂直滚动方向更改为水平

android - 使用 mipmap 会产生垃圾纹理

java - JNI UnsatisfiedLinkError - 未找到 native 方法

安卓用户界面 : Multiple Horizontally Paged Table

android - RecyclerView 2 Columns with CardView