RecyclerVIew auto scroll to display all the elements as in News Feed etc.,

前端 未结 7 714
慢半拍i
慢半拍i 2020-12-14 11:47

How to auto scroll RecyclerView smoothly so that user can see all the elements of the RecyclerView and scroll again from the start - as in News Feed etc.

<
相关标签:
7条回答
  • 2020-12-14 12:41

    You can also implement it in this way, after setting recyclerview to adapter

    final int duration = 10;
    final int pixelsToMove = 263;
    final Handler mHandler = new Handler(Looper.getMainLooper());
    
    final Runnable SCROLLING_RUNNABLE = new Runnable() {
            @Override
            public void run() {
                recyclerView.smoothScrollBy(pixelsToMove, 0);
                mHandler.postDelayed(this, duration);
            }
        };
    
    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastItem = horizontalLayoutManager.findLastCompletelyVisibleItemPosition();
                if (lastItem == horizontalLayoutManager.getItemCount() - 1) {
                    mHandler.removeCallbacks(SCROLLING_RUNNABLE);
                    Handler postHandler = new Handler();
                    postHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            quickTips.setAdapter(null);
                            quickTips.setAdapter(adapter);
                            mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);
                        }
                    }, 2000);
                }
            }
        });
        mHandler.postDelayed(SCROLLING_RUNNABLE, 2000);
    
    0 讨论(0)
提交回复
热议问题