Android Horizontal Auto scroll in Recycler view

前端 未结 2 1510
心在旅途
心在旅途 2021-01-06 07:05

I have two values in list and displaying that in horizontal list view using Recycler view. Here I need to auto scroll the horizontal list unlimited. I tried with the below c

相关标签:
2条回答
  • 2021-01-06 07:33
    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..
    
    0 讨论(0)
  • 2021-01-06 07:39

    please check the solution here. https://github.com/ritesh-bhavsar86/StockAutoScroll

    first create 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:

    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

    and

    layoutmanager is LayoutManager which set to recyclerview

    0 讨论(0)
提交回复
热议问题