简体   繁体   English

在Recycler视图中Android水平自动滚动

[英]Android Horizontal Auto scroll in Recycler view

I have two values in list and displaying that in horizontal list view using Recycler view. 我在列表中有两个值,并使用Recycler视图在水平列表视图中显示它。 Here I need to auto scroll the horizontal list unlimited. 在这里,我需要自动滚动水平列表无限制。 I tried with the below code but no result. 我尝试使用以下代码,但没有结果。

HorizontalScrollView: auto-scroll to end when new Views are added? Horizo​​ntalScrollView:添加新视图时自动滚动到结束?

please check the solution here. 请在这里查看解决方案。 https://github.com/ritesh-bhavsar86/StockAutoScroll https://github.com/ritesh-bhavsar86/StockAutoScroll

first create runnable: 首先创建runnable:

final int duration = 10;
final int pixelsToMove = 30;
private final Handler mHandler = new Handler(Looper.getMainLooper());
private final Runnable SCROLLING_RUNNABLE = new Runnable() {

    @Override
    public void run() {
        rv_autoScroll.smoothScrollBy(pixelsToMove, 0);
        mHandler.postDelayed(this, duration);
    }
};

then after setadapter() to the recyclerView use following: 然后在setadapter()到recyclerView之后使用以下命令:

rv_autoScroll.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int lastItem = layoutManager.findLastCompletelyVisibleItemPosition();
            if(lastItem == layoutManager.getItemCount()-1){
                mHandler.removeCallbacks(SCROLLING_RUNNABLE);
                Handler postHandler = new Handler();
                postHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        rv_autoScroll.setAdapter(null);
                        rv_autoScroll.setAdapter(madapter);
                        mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);
                    }
                }, 2000);
            }
        }
    });
    mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);

rv_autoScroll is recyclerview rv_autoScroll是recyclerview

and

layoutmanager is LayoutManager which set to recyclerview layoutmanager是LayoutManager,设置为recyclerview

trainigItemRV.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastItem = linearLayoutManager.findLastCompletelyVisibleItemPosition();
                if(lastItem == linearLayoutManager.getItemCount()-1){
                    mHandler.removeCallbacks(SCROLLING_RUNNABLE);
                    Handler postHandler = new Handler();
                    postHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            trainigItemRV.setAdapter(null);
                            trainigItemRV.setAdapter(productsTrainingItemAdapter);
                            mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);
                        }
                    }, 2000);
                }
            }
        });
        mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);

This Works For me..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM