12

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.

5 Answers 5

16

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");
        }
    }
});
1
  • i'm sorry, i dont get it :(
    – user9890796
    Jun 22, 2018 at 8:27
7
int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
if (firstVisiblePosition  == 0) { do your thing )
9
  • should i put it inside of my scroll listener?
    – user9890796
    Jun 22, 2018 at 8:20
  • 1
    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.
    – Zun
    Jun 22, 2018 at 8:21
  • recyclerview.findFirstVisibleItemPosition() is not working, i'm sorry. what to do.
    – user9890796
    Jun 22, 2018 at 8:30
  • I know what you should do, read my reply once again. You should use your layout manager.
    – Zun
    Jun 22, 2018 at 8:31
  • i currently have RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(mContext). i cant use findFirstVisibleItemPosition
    – user9890796
    Jun 22, 2018 at 8:38
0
   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`
                }
            }
        });
0

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();
                        }
                    }
                }
            }
        }
    });
}
3
  • i dont understand, im sorry :(
    – user9890796
    Jun 22, 2018 at 9:54
  • Where should i put the gone and visible code part? that i am said in code no have gone and visible part of code.
    – user4571931
    Jun 22, 2018 at 9:56
  • if the (n) item is visible. it should visible, else gone.
    – user9890796
    Jun 22, 2018 at 9:57
0

for avoid lagging and fix bug when user scroll slow or fast

   viewBinding.recycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {
                override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
          
    
                    if (recyclerView.scrollState == RecyclerView.SCROLL_STATE_SETTLING) {
                        if (dy > 0) {
                         //scrolled to Bottom of List

                            viewBinding.searchLayout.visibility = View.GONE
    
                        }
                        //if user scroll fast this if handle it
                        if (dy <= 0) {
                        //scrolled to Top of List

                            if (!viewBinding.recycler.canScrollVertically(-1)) {
                              //recycler view is in firstPosition
                                viewBinding.searchLayout.visibility = View.VISIBLE
                            }
                        }
                    } else if (dy <= 0) { //if user scroll slow this if handle it
                     //scrolled to Top of List

                          
                        if (!viewBinding.recycler.canScrollVertically(-1)) {
                              //recycler view is in firstPosition
                            viewBinding.searchLayout.visibility = View.VISIBLE
                        }
                    }
                }
            })

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.