0

i need help.

I wanted to change button visibility in any other item inside recyclerview to dissapear, so other button except item that i selected become invisible ...

The trigger is when item get clicked/selected then any other item's button goes invisible/gone except the one i clicked... how do i do that? im using Kotlin btw.

Here is where i want to implement it inside "if (!b)" (if possible) in onBindViewHolder:

holder.mainLayout.setOnClickListener {
           
            if (!b){
                b = true
                holder.addsLayout.isVisible = true

            } else {
                b = false
                holder.addsLayout.isVisible = false
            }
        }

Edit:

Here is my viewholder:

class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        val itemTitle: TextView = itemView.findViewById(R.id.itemTitleRecordLocal)
        val itemDelete: Button = itemView.findViewById(R.id.recordlocalDelete)
        val itemTranscribe: Button = itemView.findViewById(R.id.transcribelocalTxt)
        val mainLayout: RelativeLayout = itemView.findViewById(R.id.recordlocalTitleDateLayout)
        val addsLayout: RelativeLayout = itemView.findViewById(R.id.addsRecordLocalLayout)
}

I just updated my code earlier, so its not button anymore but layout, but i think its similar bcuz i just need to change the visibility.

  1. itemDelete and itemTranscribe are buttons that i want to hide, and i put it in addsLayout.
  2. mainLayout is the item where i need to click to be selected.
Dioo B
  • 82
  • 9
  • Can you explain more clearly? What are expectations and what have you implemented so far? Attach some video/image of your view or something. – krupa parekh Nov 25 '22 at 04:34
  • use to indexes -> one current selected position and other the previous selected index. when you click change the current previous and call notifyItemChangedAtPosition for those two poisitons. I am not sure if i understand your requirement clearly. – Raghunandan Nov 25 '22 at 05:58
  • @krupaparekh for example when i click current item in recyclerview, any other item that i didn't click hiding their button... – Dioo B Nov 25 '22 at 06:52
  • @Raghunandan i know its difficult to explain.. for example if i click item number 5th, all other item that not 5th item hide their button or their button become invisible – Dioo B Nov 25 '22 at 06:54
  • @DiooB As I understand you want that if item 5 is selected then all other items should hide button and only item 5 should show button. Am i correct? – krupa parekh Nov 25 '22 at 06:57
  • @krupaparekh correct... the trigger is when item selected/clicked... – Dioo B Nov 25 '22 at 07:06
  • @DiooB Can you provide the whole adapter code you implement and UI also? – krupa parekh Nov 25 '22 at 07:09

1 Answers1

2

As per your code, I understand that you are passing any data class to the recycler view adapter of type suppose myData

Here is what you can do:

Take one boolean variable in your data class item, say isSelected, default value will be false.

Then whenever you get click of your mainLayout, change the value of that boolean.

Check condition according to that boolean and manage visibility according to that.

Also, best practice is you manage your clickevents in activity/fragment. So my suggestion is give callback of click of mainLayout to the activity/fragment and perform the next operations there only.

After that, in activity/fragment you need to make for loop and make isSelected false for all data except the one you selected.

For example in my case I have one music player recyclerview and need to change icon if one is being played then all others should be pause. So I need to change icon based on that condition. My data class contain one variable isPlaying.

In adapter:

// Inside view holder
if (data.isPlaying) {
    // set image of play
} else {
   // set image of pause
}

In on click, I have make one mutable live data and observe that in activity/fragment and get callback of click there.

Inside activity/fragment:

adapter.getListItems().forEach {
                        it.isPlaying = false
                    }
data.isPlaying = true
notifyDataSetChanged()
krupa parekh
  • 607
  • 3
  • 16