JayVDiyk
JayVDiyk

Reputation: 4487

How to know last item in RecyclerView

In my RecyclerViewAdapter, I would like to know if this item is the last. How can I check it?

The onBindViewHolder only has the position value

@Override
public void onBindViewHolder(UserViewHolder userViewHolder, int position) {

       //DO SOMETHING TO THE VIEW IF THIS IS THE LAST ITEM IN RECYCLERVIEW
}

Upvotes: 8

Views: 16578

Answers (2)

Lazy Ninja
Lazy Ninja

Reputation: 22537

You can use getItemCount() or the size of your adapter list.

@Override
public void onBindViewHolder(UserViewHolder userViewHolder, int position) {

       if( position == getItemCount() - 1 ){
          // Your last item
       }
}

Upvotes: 9

Kosh
Kosh

Reputation: 6334

simply you can do this:

    if(position == myList.size()-1){/*lastItem*/}

Upvotes: 12

Related Questions