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

前端 未结 7 719
慢半拍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:21

    After Trial And Errors This Works Perfect For Me

        final RecyclerView mRecyclerViewr;
        final ArrayList stringArrayData = new ArrayList
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
        mRecyclerViewr.setLayoutManager(linearLayoutManager);
        CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), topPriceBarList);
        mRecyclerViewr.setAdapter(customAdapter);
    
            // Auto Scroll Left To Right
            final int scrollSpeed = 100;   // Scroll Speed in Milliseconds
            final Handler handler = new Handler();
            final Runnable runnable = new Runnable() {
                int x = 15;        // Pixels To Move/Scroll
                boolean flag = true;
                // Find Scroll Position By Accessing RecyclerView's LinearLayout's Visible Item Position
                int scrollPosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();  
                int arraySize = stringArrayData.size();  // Gets RecyclerView's Adapter's Array Size
    
                @Override
                public void run() {
                    if (scrollPosition < arraySize) {
                        if (scrollPosition == arraySize - 1) {
                            flag = false;
                        } else if (scrollPosition <= 1) {
                            flag = true;
                        }
                        if (!flag) {
                            try {
                                // Delay in Seconds So User Can Completely Read Till Last String
                                TimeUnit.SECONDS.sleep(1); 
                                mRecyclerViewr.scrollToPosition(0);  // Jumps Back Scroll To Start Point
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        // Know The Last Visible Item
                        scrollPosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();
    
                        mRecyclerViewr.smoothScrollBy(x, 0);
                        handler.postDelayed(this, scrollSpeed);
                    }
                }
            };
            handler.postDelayed(runnable, scrollSpeed);
    

    This Will Auto Scroll Your RecyclerView To The End, Wait For a Second (So User Can Read Till End) And Jumps/Scroll Back To First String In RecyclerView's Array List. If You Want To Auto Scroll in Posetive And Negative direction You Just Need To Change The Condition Instead Of Using if(!flag) You Need To Set Value Of x in it

        if (flag) x = 15;
        else x= -15;  // For Auto Scroll To Negative i.e Left Direction
    

提交回复
热议问题