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

android-recyclerview detect scroll end
recyclerview scrolling to top after calling notifydatasetchanged
android-recyclerview detect scroll stop
recyclerview scroll up and down listener
auto scroll recyclerview android
recyclerview automatically scrolls to the bottom when data is loaded
recyclerview scroll up and down data will change
android-recyclerview update without scroll

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

private void laySwipeToggle() {
    if (mRecyclerView.getChildCount() == 0 || mRecyclerView.getChildAt(0).getTop() == 0) {
        mLaySwipe.setEnabled(true);
    } else {
        mLaySwipe.setEnabled(false);
    }
}

But here is the problem. When it's scrolled to another item's view's boundary mRecyclerView.getChildAt(0).getTop() also returns 0.

Is there something like RecyclerView.isScrolledToBottom() or RecyclerView.isScrolledToTop()?

EDIT: (mRecyclerView.getChildAt(0).getTop() == 0 && linearLayoutManager.findFirstVisibleItemPosition() == 0) kind of does the RecyclerView.isScrolledToTop(), but what about RecyclerView.isScrolledToBottom()?

The solution is in the layout manager.

LinearLayoutManager layoutManager = new LinearLayoutManager(this);

// Add this to your Recycler view
recyclerView.setLayoutManager(layoutManager);

// To check if at the top of recycler view
if(layoutManager.firstCompletelyVisibleItemPosition()==0){
    // Its at top
}

// To check if at the bottom of recycler view
if(layoutManager.lastCompletelyVisibleItemPosition()==data.size()-1){
    // Its at bottom
}

EDIT

In case your item size is larger than the screen use the following to detect the top event.

RecyclerView recyclerView = (RecyclerView) view;
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();

int pos = linearLayoutManager.findFirstVisibleItemPosition();

if(linearLayoutManager.findViewByPosition(pos).getTop()==0 && pos==0){
    return true;
}

PS: Actually, if you place the RecyclerView directly inside the SwipeRefreshview you wouldn't need to do this

Detect when RecyclerView reaches the bottom most position while , setLayoutManager(layoutManager); //Tocheck if recycler is on top Answers: In order to check whether RecyclerView is scrolled to bottom. Use recyclerView.canScrollVertically(int direction) to check if top or bottom of the scroll reached. direction = 1 for scroll down (bottom) direction = -1 for scroll up (top) if method return false that means you reached either top or bottom depends on the direction.

Android: Detect when the RecyclerView reaches the bottom, Polymorphism. Few days ago i was trying to implement a way to make my app fetch data from the server while the user is scrolling since there's  Use recyclerView.canScrollVertically(int direction) to check if top or bottom of the scroll reached. direction = 1 for scroll down (bottom) direction = -1 for scroll up (top) if method return false that means you reached either top or bottom depends on the direction.

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;
            }
        });

RecyclerView automatically scrolls to the bottom when data is , I was trying to implement a vertical recyclerview laid out from top to bottom but when the data got loaded the list always scrolled to the bottom. However, you can check a few things: do you have the LinearLayoutManager  Top network posts 105 How to know whether a RecyclerView / LinearLayoutManager is scrolled to top or bottom? 39 Difference between the main thread and UI thread

@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...
                }
            }
        });

LinearLayoutManager, Checks if RecyclerView is in the middle of a layout or scroll and throws an LayoutManager should not check whether the position exists in the adapter or not​. layout such that item[10] 's bottom is 20 pixels above the RecyclerView's bottom. Detect when RecyclerView reaches the bottom most position while scrolling (8) Try This I have used above answers it runs always when you will go at the end of recycler view,

Use recyclerView.canScrollVertically(int direction) to check if top or bottom of the scroll reached.

direction = 1 for scroll down (bottom)

direction = -1 for scroll up (top)

if method return false that means you reached either top or bottom depends on the direction.

RecyclerView reverse endless scrolling, android recyclerview dynamically load more items when scroll to end with bottom progressbar manner, like start from the bottom and scroll up, when top is reached, load more. layoutManager = new LinearLayoutManager(this); recyclerView more data, // but first we check if we are waiting for the previous load to finish. Sheraz Ahmad Khilji. 32 How to know whether a RecyclerView / LinearLayoutManager is scrolled to top or bottom? View more network posts →

How to Scroll top in RecyclerView with LinearLayoutManager in , Before getting into example, we should know what is Recycler view in android. "wrap_content" android:layout_gravity = "bottom|end" android:src When you click on FloatingActionButton, it will scroll to top position. You will learn to “handle button click inside a row in recyclerview” in RecyclerView Button Android tutorial. If you have not implement basic recyclerview with custom adapter yet, check recyclerview android example first. Other useful tutorials, Android Recyclerview Search Filter Example. Android Recyclerview With Edittext.

Android RecyclerView Load More, Endless Scrolling, How to create infinite scrolling RecyclerView items in android app example. LinearLayoutManager" app:layout_constraintBottom_toBottomOf="parent" Let's look at the MainActivity.java class where we instantiate the above Adapter. It's there where we check the scrolled state of the RecyclerView and if the bottom-​most  RecyclerView is an important graphical UI component in any android application. Android provides RecyclerView.Adapter class which manages all the functionality of RecyclerView. I don’t know why but android people have kept this class in a very abstract form with only basic functionalities implemented by default.

Endless Scrolling with AdapterViews and RecyclerView, This data can be used to decide when to load more data from an external source: of endless scrolling, it's important to fetch data before the user gets to the end of the list. linearLayoutManager = new LinearLayoutManager(this); rvItems. See Implementing with RecyclerView section for the most recent and relevant  In the Open Event Android app we only had a single data source for searching in each page that was the content on the page itself. But it turned out that users want to search data across an event and therefore across different screens in the app. Global search solves this problem.

Comments
  • I suppose the latter can be achieved by verifying the bottom of the recyclerview against the last child i.e something on the lines of mRecyclerView.getBottom()== linearLayoutmanager.findViewbyPosition(adapter.getItemCount() - 1).getBottom()
  • @Saren Arterius you might want to take a look at this
  • No, it still happens when the RecyclerView is the direct child of SwipeRefreshLayout. But checking the firstCompletelyVisibleItemPosition fixes the problem. I need to point something here. If none of the items are fully visible firstCompletelyVisibleItemPosition() returns -1. So this solution will not work if the first item doesn't fit into the RecyclerView when it is scrolled to top. But it is a very rare situation.
  • @eluleci : Check for whether list is on top RecyclerView rc=(RecyclerView)view; LinearLayoutManager lm= (LinearLayoutManager) rc.getLayoutManager(); if(lm.findViewByPosition(lm.findFirstVisibleItemPosition()).getTop()==0 && lm.findFirstVisibleItemPosition()==0) return true; //This would work evn if the item is of a larger size than screen
  • This would fail if the item (first or last) is larger than the entire screen. Because, first/lastCompletelyVisibleItemPosition() will return -1, implies, there is no fully visible item at all.
  • Both lastVisibleItemPosition and lastCompletelyVisibleItemPosition don't work. (cannot resolve method). Help?
  • findLastCompletelyVisibleItemPosition will FAIL if the last item's height is greater than the screen height. It means that the last item is visible, but not completely visible, so layout.findLastCompletelyVisibleItemPosition()=-1(RecyclerView.NO_POSITION). Use lastVisibleItemPosition and check for the bottom of the last view in layout will be safer.
  • This would have to be repeated on an interval though, right?
  • You can out this in recyclerView.addOnScrollListener..it works like a charm...thanks
  • Great! This returns a false when we reach the end of scrolling, I needed this event.
  • Excellent. Use this in onScrollStateChanged works for me.
  • direction integers: -1 for up, 1 for down, 0 will always return false.
  • Worked for me. I had to add it to the RecyclerView's addOnScrollListener() though
  • this doesnt work for the trivial case where there is one item in the recyclerview and it is fully visible.