Change visibility of a view in all items of recyclerview

前端 未结 2 1848
走了就别回头了
走了就别回头了 2021-01-06 05:03

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

相关标签:
2条回答
  • 2021-01-06 05:22

    You could create a boolean in your adapter that stores the information if the there was a long press or not and in your onBindViewHolder you check whether this is true or false and set the visibility according to it: holder.chk_faviorateVideo.setVisibility(longPressed ? View.VISIBLE : View.GONE); Then in your click listener you toggle this variable and call notifyDataSetChanged()

    0 讨论(0)
  • 2021-01-06 05:29

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

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

    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.

    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.

    0 讨论(0)
提交回复
热议问题