RecyclerView物品未正确涂漆 [英] RecyclerView items are not painted properly

查看:93
本文介绍了RecyclerView物品未正确涂漆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ViewHolder中,我有此方法,当 onBindViewHolder :

In my ViewHolder, I have this method, that is called in the adapter when onBindViewHolder:

protected void bindViews(Item item) {
    dateTextView.setText(item.getDate());
    titleTxtView.setText(item.getTitle());
    if (item.hasMenu()) {
        Log.d("tag", "Item " + item.getId() + " has menu");
        menuButton.setVisibility(VISIBLE);
    } else {
        menuButton.setVisibility(INVISIBLE);
    }
}

但是有些 Item 没有菜单(即 hasMenu()返回false),但显示了 menuButton ,反之反之亦然.

But there are Items that doesn't have menu (that is hasMenu() returns false) but menuButton is shown and vice versa.

我认为是因为物品回收,因为我有100个物品,而只有2个有菜单.打印:

I think is because the item recycling because I have 100 items and just 2 have menu. It prints:

第31项具有菜单

Item 31 has menu

第78项具有菜单

没关系.但是,在该应用程序中,我可以看到其他一些项目正在显示 menuButton ,而它们不应该显示.

That's ok. However, in the app I can see that some other items are showing the menuButton and they shouldn't.

当显示/隐藏 menuButton 时,我是否应该强制重涂每个项目?如果是这样,我该怎么办?

Should I force repaint each item when show/hide the menuButton? If so, how can I do it?

推荐答案

使用 public int getItemViewType(int position) public int getItemCount()来填充两个不同的布局,一个带菜单,另一个不带菜单.

Use public int getItemViewType(int position) and public int getItemCount() to inflate two different layout,one with menu and other without the menu.

这篇关于RecyclerView物品未正确涂漆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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