在recyclerview的所有项目中更改视图的可见性 [英] Change visibility of a view in all items of recyclerview

查看:123
本文介绍了在recyclerview的所有项目中更改视图的可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的recyclerview项目中有一个CheckBox,可见性已消失.当我对一个项目进行longClick时,我想使它对recyclerview中的所有项目可见.所以我这样做了:

I have a CheckBox in my recyclerview items with Gone visibility . I want to make it visible for all items in recyclerview when I have a longClick on one of items .so I did this :

    holder.faviorateVideoItemRelative.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View arg0) {
            holder.chk_faviorateVideo.setVisibility(View.VISIBLE);
           // holder.chk_faviorateVideo.setChecked(!holder.chk_faviorateVideo.isChecked());
            return  true ;
        }
    });

但是这将使CheckBox仅对当前item可见. 我什至将代码移到了ViewHolder类中,但没有任何改变.如何在没有循环或重新绘制recyclerview的情况下做到这一点? 我需要执行一些操作,例如长按并选择android的默认文件管理器. 提前致谢 .

but this will make CheckBox visible just for current item . I even moved my code to my ViewHolder class, but nothing changed . how can I do that without loop or redrawing of recyclerview ? I need to do something like android's default file manager long press and selecting . thanks in advance .

推荐答案

您的Holder仅保存当前项目,这就是为什么您仅在此项目中看到结果的原因.

Your Holder holds only current item, that's why you see a result in this item only.

第一个选择是创建一个布尔标志并调用notifyDataSetChanged(),在onBindViewHolder()中,只需使用该标志设置可见性即可.

First option is just to make some boolean flag and call notifyDataSetChanged(), in onBindViewHolder() just use that flag to set visibility.

此外,您也可以尝试创建CheckBoxesHolders的集合,并长按鼠标以对其进行迭代以使复选框可见.对我来说,这不是最糟糕的选择.

Also you can try to create collection of your CheckBoxes or Holders and on long click iterate them to make checkboxes visible. As for me, it's not the worst option.

如果您不想为此循环,我认为另一种方法是实现观察器模式-创建复选框时,您应该将其添加为订阅者,以接收可见性通知.

If you don't want to have a loop for that, another way I see, is to implement Observer pattern - on checkbox creation you should add it as a subscriber to receive visibility notification.

在这里也很有可能使用RX:
-长按会发出事件
-并且在您订阅该事件的复选框之前.
它也与Observer模式有关.

Also there is a good possibility to use RX here:
- on long click you emit event
- and before you subscribe your checkboxes on that event.
It's also related to Observer pattern.

这篇关于在recyclerview的所有项目中更改视图的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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