How to know whether a RecyclerView / LinearLayoutManager is scrolled to top or bottom?

后端 未结 11 2052
一向
一向 2020-12-02 06:24

Currently I am using the follow code to check whether SwipeRefreshLayout should be enabled.

private void laySwipeToggle() {
    if (mRecyclerView.getChildCou         


        
相关标签:
11条回答
  • 2020-12-02 06:45

    you can do this it's work for me

          mRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                }
            }
    
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    int topRowVerticalPosition = (recyclerView == null || recyclerView.getChildCount() == 0) ?
                            0 : recyclerView.getChildAt(0).getTop();
                    LinearLayoutManager linearLayoutManager1 = (LinearLayoutManager) recyclerView.getLayoutManager();
                    int firstVisibleItem = linearLayoutManager1.findFirstVisibleItemPosition();
                    swipeRefreshLayout.setEnabled(firstVisibleItem == 0 && topRowVerticalPosition >= 0);
                }
            });
    
    0 讨论(0)
  • 2020-12-02 06:48

    @Saren Arterius, Using the addOnScrollListener of RecycleView, you can find the scrolling top or bottom of Vertical RecycleView like below,

    RecyclerView rv = (RecyclerView)findViewById(R.id.rv);
    
    rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
    
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                }
    
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    
                    if (dy < 0) {
                        // Recycle view scrolling up...
    
                    } else if (dy > 0) {
                        // Recycle view scrolling down...
                    }
                }
            });
    
    0 讨论(0)
  • 2020-12-02 06:52

    You can detect top by using the following

     recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
              }
    
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(IsRecyclerViewAtTop())
                {
                    //your recycler view reached Top do some thing
                 }
            }
        });
    
      private boolean IsRecyclerViewAtTop()   {
            if(recyclerView.getChildCount() == 0)
                return true;
            return recyclerView.getChildAt(0).getTop() == 0;
        }
    

    This will detect top when you release the finger once reached top, if you want to detect as soon the reacyclerview reaches top check if(IsRecyclerViewAtTop()) inside onScrolled method

    0 讨论(0)
  • 2020-12-02 06:54

    To find out if the RecyclerView is scrolled to bottom, you could reuse the methods used for the scrollbar.

    This is the calculation, for convenience written as a Kotlin extension function:

    fun RecyclerView.isScrolledToBottom(): Boolean {
        val contentHeight = height - (paddingTop + paddingBottom)
        return computeVerticalScrollRange() == computeVerticalScrollOffset() + contentHeight
    }
    
    0 讨论(0)
  • 2020-12-02 06:56

    You can try recyclerView.canScrollVertically(int direction), if you just need to know whether it possible to scroll or not.

    Direction integers:

    • -1 for up
    • 1 for down
    • 0 will always return false.
    0 讨论(0)
  • 2020-12-02 07:03

    In order to check whether RecyclerView is scrolled to bottom. Use the following code.

    /**
         * Check whether the last item in RecyclerView is being displayed or not
         *
         * @param recyclerView which you would like to check
         * @return true if last position was Visible and false Otherwise
         */
        private boolean isLastItemDisplaying(RecyclerView recyclerView) {
            if (recyclerView.getAdapter().getItemCount() != 0) {
                int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
                if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
                    return true;
            }
            return false;
        }
    

    Some Additional info

    If you want to implement ScrollToBottom in RecyclerView when Edittext is tapped then i recommend you add 1 second delay like this:

     edittext.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP)
                        if (isLastItemDisplaying(recyclerView)) {
    // The scrolling can happen instantly before keyboard even opens up so to handle that we add 1 second delay to scrolling
                            recyclerView.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    recyclerView.smoothScrollToPosition(recyclerView.getAdapter().getItemCount() - 1);
    
                                }
                            }, 1000);
                        }
                    return false;
                }
            });
    
    0 讨论(0)
提交回复
热议问题