How to check if first item of RecyclerView is visible on the screen

Is it possible to check if the first or second item of the RecyclerView is visible on the screen of the user?

For example when the user scrolls down:

  if (first item not visible to user)  { 
      // do something
  }
  else if ( first item is visible){
      // do something
  }

What I currently do is I add a listener to my recycler so that when the user scrolls down, it will do something and scroll up.

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if (dy > 0) {
            mAccountLayout.setVisibility(View.GONE);
            mDateLayout.setVisibility(View.GONE);
            Log.d("SCROLLINGDOWN","SCROLL");
        } else {
            mAccountLayout.setVisibility(View.VISIBLE);
            mDateLayout.setVisibility(View.VISIBLE);
            Log.d("SCROLLINGUP","SCROLL");
        }
    }
});

But what I need is to check if the first item is visible or not.

You can find some helper methods in RecyclerView.LayoutManager, for example, if you use a LinearLayoutManager, check these methods:

int findFirstCompletelyVisibleItemPosition() // Returns the adapter position of the first fully visible view.
int findFirstVisibleItemPosition() // Returns the adapter position of the first visible view.
int findLastCompletelyVisibleItemPosition() // Returns the adapter position of the last fully visible view.
int findLastVisibleItemPosition() // Returns the adapter position of the last visible view.

See the full docs here.

In your code:

recyclerView.setAdapter(adapter);
final LinearLayoutManager layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        if (layoutManager.findFirstVisibleItemPosition() > 0) {
            mAccountLayout.setVisibility(View.GONE);
            mDateLayout.setVisibility(View.GONE);
            Log.d("SCROLLINGDOWN","SCROLL");
        } else {
            mAccountLayout.setVisibility(View.VISIBLE);
            mDateLayout.setVisibility(View.VISIBLE);
            Log.d("SCROLLINGUP","SCROLL");
        }
    }
});

Detecting List Items Observed by User, Quite often, the business wants to know if the user has viewed and perceived some Distinct: skip the event when the visible item set is equal to the one been just The RecyclerView itself is only a structure to provide a limited window to the  The idea here is to find the position of the last visible item. If that position is equal to the last item of your dataset, then you should trigger a reload. There are several methods to accomplish it.

int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
if (firstVisiblePosition  == 0) { do your thing )

Detect if item is visible in recyclerview from adapter [duplicate , How can I accomplish this? You would need to listen to the scrolling of your RecyclerView and get the first item displayed onto the screen. Thanksfully, Android let  Espresso – Testing RecyclerViews at Specific Positions My team recently added a RecyclerView to a screen in an Android app we’re working on. It’s a horizontal view that allows a user to scroll left and right to see content that’s offscreen.

   declare globally LinearLayoutManager layoutManager;

and in oncreate use

     layoutManager = new LinearLayoutManager(this);

use this layoutmanager in recyclerview
   recyclerview.setLayoutManager(layoutManager);

        recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                //Tocheck if  recycler is on top
                if (layoutManager.findFirstCompletelyVisibleItemPosition() == 0) {
//Its at top ..
                    //ie first item is visible
                } else {
             //not visible`enter code here`
                }
            }
        });

Improve RecyclerView Performance - Noteworthy, If you google it you can find some useful solutions to improve the performance but in RecyclerView doesn't scroll smoothly; Some first items scrolled slowly the RecyclerView such that there's a view that is just barely completely off-screen, the RecyclerView will attempt to synthesize visible structural change events for​  Building a view is a heavy operation for the Android system. Therefore, in a list which could potentially hold hundreds of elements, the system will create only the number of views visible on screen and no more. When you swipe to see others and one of the previously created view disappears, it is “recycled” so that it can be reused.

Try this things..

    private LinearLayoutManager linearLayoutManager;// this two line define as global level.
protected int pastVisibleItems, visibleItemCount, totalItemCount;
protected void addScrollListener() {
    rvData.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (dy > 0) //check for scroll down
            {
                visibleItemCount = linearLayoutManager.getChildCount();
                totalItemCount = linearLayoutManager.getItemCount();
                pastVisibleItems = linearLayoutManager.findFirstVisibleItemPosition();

                if (!isPullingMoreResults) {
                    if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
                        if (mHasMoreResultsToPull && !isPullingMoreResults) {
                            isPullingMoreResults = true;
                            pageNumber++;
                            getMessage();
                        }
                    }
                }
            }
        }
    });
}

RecyclerView position helper to get first and last visible positions , @return The adapter position of the first visible item or {@link RecyclerView#​NO_POSITION} if. * there aren't any visible items. */. public int  Most of the available applications today always have a screen where they show us a list of items. The main screen of WhatsApp provides us with a list of chat histories and contacts, Youtube

Detecting List Items Observed by User, Distinct: skip the event when the visible item set is equal to one that has just The RecyclerView itself is only a structure to provide a limited window to the list of items. To detect scroll events in RecyclerView we need to add scroll listener ZalandoZalando TechRxJavaMobileMobile FirstUIJavaAndroid  The RecyclerView uses a layout manager to position the individual items on the screen and determine when to reuse item views that are no longer visible to the user. To reuse (or recycle ) a view, a layout manager may ask the adapter to replace the contents of the view with a different element from the dataset.

Android: Detect when the RecyclerView reaches the bottom, Sometimes we need to detect weather a RecyclerView hits the bottom for alot of reasons, Create an item view in your res/layout folder, i called it my_item.xml. So you need to augment your RecyclerView to snap to the first visible item in the list. Or maybe you need it to snap to the item closest to the middle. Or maybe some other variation. One option might be to attach a RecyclerView.OnFlingListener and intercept the events. That’s a lot of work and easy to overlook some things.

RecyclerView.LayoutManager, A LayoutManager is responsible for measuring and positioning item views within a the policy for when to recycle item views that are no longer visible to the user. Check if the RecyclerView is configured to clip child views to its padding. which positions this LayoutManager will first need, once it appears on the screen. Sometimes we need to detect weather a RecyclerView hits the bottom for alot of reasons, one of the most frequent is when you need to show more content of your app (pagination) when the user scrolls…

Comments
  • i'm sorry, i dont get it :(
  • should i put it inside of my scroll listener?
  • Check whatever event works best for you. Just a tip, don't put too many if-statements or actions in the listener because the event is fired multiple times when the user scrolls.
  • recyclerview.findFirstVisibleItemPosition() is not working, i'm sorry. what to do.
  • I know what you should do, read my reply once again. You should use your layout manager.
  • i currently have RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(mContext). i cant use findFirstVisibleItemPosition
  • i dont understand, im sorry :(
  • Where should i put the gone and visible code part? that i am said in code no have gone and visible part of code.
  • if the (n) item is visible. it should visible, else gone.