Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Limit Number of Items in RecyclerView?

How can I limit the number of items displayed by the RecyclerView ?

I can limit the amount inserted it if I override getChildCount, but that causes it to only insert that number and then stop. I want it to keep inserting/scrolling, but only display X number of items.

(Note: The height of each item can differ, so it's important that the limit is based on quantity rather than some hard coded value.)

like image 957
Elliptica Avatar asked Jun 15 '18 00:06

Elliptica


2 Answers

Inside your Recyclerview's adapter class;

private final int limit = 10;


 @Override
public int getItemCount() {
    if(ad_list.size() > limit){
        return limit;
    }
    else
    {
        return ad_list.size();
    }

}
like image 135
jhashane Avatar answered Sep 26 '22 23:09

jhashane


In your Adapter, just set a limit,

   @Override
        public int getItemCount() {
            int limit = 5;
            return Math.min(diseasesList.size(), limit);
    
        }
like image 44
Kamau Mbûgua Avatar answered Sep 23 '22 23:09

Kamau Mbûgua