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

时间:2016-08-31 07:10:43

标签: android

我在列表中有两个值,并使用Recycler视图在水平列表视图中显示该值。在这里,我需要自动滚动水平列表无限制。 我尝试使用以下代码,但没有结果。

HorizontalScrollView: auto-scroll to end when new Views are added?

感谢回复...

2 个答案:

答案 0 :(得分:11)

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

  

首先创建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);
    }
};
  

然后在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是recyclerview

layoutmanager是LayoutManager,设置为recyclerview

答案 1 :(得分:0)

group by