Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Detect when the last item in a RecyclerView is visible

I have a method that will check if the last element in a RecyclerView is completely visible by the user, so far I have this code The problem is how to check if the RecyclerView has reached it's bottom ?

PS I have items dividers

public void scroll_btn_visibility_controller(){
    if(/**last item is visible to user*/){
        //This is the Bottom of the RecyclerView
        Scroll_Top_Btn.setVisibility(View.VISIBLE);
    }
    else(/**last item is not visible to user*/){
        Scroll_Top_Btn.setVisibility(View.INVISIBLE);
    }
}

UPDATE : This is one of the attempts I tried

boolean isLastVisible() {
    LinearLayoutManager layoutManager = ((LinearLayoutManager)rv.getLayoutManager());
    int pos = layoutManager.findLastCompletelyVisibleItemPosition();
    int numItems =  disp_adapter.getItemCount();
    return (pos >= numItems);
}
public void scroll_btn_visibility_controller(){

    if(isLastVisible()){
        Scroll_Top.setVisibility(View.VISIBLE);
    }
    else{
        Scroll_Top.setVisibility(View.INVISIBLE);
    }
} 

so far no success I think there is something wrong within these lines :

int pos = layoutManager.findLastCompletelyVisibleItemPosition();
int numItems =  disp_adapter.getItemCount();
like image 392
Thorvald Avatar asked Nov 21 '16 17:11

Thorvald


People also ask

How can I check if a recyclerView item is visible?

You could make some logic using LayoutManager api to get last completely visible item position in RecyclerView onScrolled method: ((LinearLayoutManager) vYourRecycler. getLayoutManager()). findLastCompletelyVisibleItemPosition();

How do I get last position in adapter?

get(myFriends. size() - 1); will give you the last item. myFriends. size() - 1 is the last item position.

Does flutter have recyclerView?

The Complete Flutter Series is a series of articles focused on cross-platform app development on the Flutter framework for everyone from beginners to experienced mobile developers.


4 Answers

You can create a callback in your adapter which will send a message to your activity/fragment every time when the last item is visible.

For example, you can implement this idea in onBindViewHolder method

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
    if(position==(getItemCount()-1)){
        // here goes some code
        //  callback.sendMessage(Message);
     }
    //do the rest of your stuff 
}

UPDATE

Well, I know it's been a while but today I ran into the same problem, and I came up with a solution that works perfectly. So, I'll just leave it here if anybody ever needs it:

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        LinearLayoutManager layoutManager=LinearLayoutManager.class.cast(recyclerView.getLayoutManager());
        int totalItemCount = layoutManager.getItemCount();
        int lastVisible = layoutManager.findLastVisibleItemPosition();

        boolean endHasBeenReached = lastVisible + 5 >= totalItemCount;
        if (totalItemCount > 0 && endHasBeenReached) {
            //you have reached to the bottom of your recycler view
        }
    }
});
like image 157
nullbyte Avatar answered Oct 23 '22 14:10

nullbyte


You should use your code with following change:

boolean isLastVisible() {
    LinearLayoutManager layoutManager =((LinearLayoutManager) rv.getLayoutManager());
    int pos = layoutManager.findLastCompletelyVisibleItemPosition();
    int numItems = rv.getAdapter().getItemCount();
    return (pos >= numItems - 1);
}

Be careful, findLastCompletelyVisibleItemPosition() returns the position which start at 0. So, you should minus 1 after numItems.

like image 17
Richard Avatar answered Oct 23 '22 16:10

Richard


Assuming you're using LinearLayoutManager, this method should do the trick:

boolean isLastVisible() {
  LinearLayoutManager layoutManager = ((LinearLayoutManager)mRecyclerView.getLayoutManager());
  int pos = layoutManager.findLastCompletelyVisibleItemPosition();
  int numItems = mRecyclerView.getAdapter().getItemCount();
  return (pos >= numItems);
}
like image 15
marmor Avatar answered Oct 23 '22 15:10

marmor


try working with onScrollStateChanged it will solve your issue

like image 5
Metronom Avatar answered Oct 23 '22 14:10

Metronom