如何限制RecyclerView中的项目数? [英] How to Limit Number of Items in RecyclerView?

查看:549
本文介绍了如何限制RecyclerView中的项目数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何限制RecyclerView 显示的项目数量?

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

如果我覆盖getChildCount,我可以限制插入的数量,但这会导致它仅插入该数字然后停止.我希望它继续插入/滚动,但只有 display X个项目.

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

推荐答案

在Recyclerview的适配器类内部;

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

}

这篇关于如何限制RecyclerView中的项目数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆