15

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.)

Elliptica
  • 3,928
  • 3
  • 37
  • 68

7 Answers7

30

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();
    }

}
jhashane
  • 774
  • 1
  • 9
  • 17
3

One solution is to set width of each child programmatically,

@Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.MY_ITEM_TO_INFALTE, parent, false);

        itemView.getLayoutParams().width = (int) (getScreenWidth() / NUMBERS_OF_ITEM_TO_DISPLAY); /// THIS LINE WILL DIVIDE OUR VIEW INTO NUMBERS OF PARTS

        return new MyCreationItemAdapter.MyViewHolder(itemView);
    }

    public int getScreenWidth() {

        WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);

        return size.x;
    }
Ranjith KP
  • 858
  • 6
  • 18
3

In your Adapter, just set a limit,

   @Override
        public int getItemCount() {
            int limit = 5;
            return Math.min(diseasesList.size(), limit);
    
        }
Kamau Mbûgua
  • 777
  • 1
  • 10
  • 19
2

You can simply override getCount() method and test if adapter data array is superior than "DESIRED_MAX" then return "DESIRED_MAX" else just return the data array length or size (array/ArrayList)

AouledIssa
  • 2,528
  • 2
  • 22
  • 39
1

if you want it to keep inserting/scrolling, but only display X number of items, then I have a solution: setVisibility to VISIBLE for X items, and other is GONE.

public static final class ViewHolder extends RecyclerView.ViewHolder {
    public ViewHolder(View view) {
        super(view);
        handleShowView(view);
    }

    private void handleShowView(View view) {
        if (getAdapterPosition() > X - 1) {
            view.setVisibility(View.GONE);
            return;
        }
        view.setVisibility(View.VISIBLE);
    }
}

hope this helps

GianhTran
  • 3,443
  • 2
  • 22
  • 42
0

What you can do is find the dp it takes for one of your list items in the recycler view. Ex: 150dp ea/item.

then in the XML:

<RecyclerView
....
android:layout_height="wrap_content"
app:layout_constraintHeight_max="300dp" 
/>

This means if you have < 300dp / approximately 2 items inside the recycler view, it will wrap content, but if it exceeds 300dp / more than 2 items, it will recycle the whole view while displaying 2 at a time.

0

***warning with latest libs from 01/2023 (androix, recyclerview, etc) This "might" have worked, but maybe not anymore...

I am still investigating, but this seems to lead to exceptions:

@Override
public int getItemCount() {
    return Math.min(mDiffer.getCurrentList().size(),mListLimit);
}

The second I load more items into the list then "mListLimit" allows, it crashes my app. I have to load itemcount>mlistlimit at once and the line above will throw mentioned exception. if I just use :

@Override
public int getItemCount() {
    return mDiffer.getCurrentList().size();
}

then I get no exception. Mind you right now it has to very specific values: listlimit is 300. I load 20 items. Then I load 1000, which will lead to "overflow" the allowed list, only then it will crash.

Maybe this helps someone.

CaptainCrunch
  • 1,230
  • 14
  • 15