Home > Blockchain >  How to trigger other CardView element in recyclerview than clicked?
How to trigger other CardView element in recyclerview than clicked?

Time:05-14

I have a method in adapter using recyclerview I basically display some elements of cardViews.

How to trigger method on other element where usually I use

cardView.setOnClickListener {
// MY action on specific} cardView element

for example when I click third element, <MY ACTION> happens in second element? Inside cardView I can i.e use:

statusButtons.toggleVisibility(!statusButtons.isVisible)

but it will only work when I trigger actual element using method

cardView.setOnClickListener { }

I have found something similiar How to access all cardviews in a recyclerview, on item click of a particular cardview

but it is not directly related to my case, I need to make action on specific ID like

statusButtons.toggleVisibility(!statusButtons.isVisible) where I not have argument to change

CodePudding user response:

You can use the RecyclerView.findViewHolderForLayoutPosition(position) function to get the ViewHolder at given position and call functions on that ViewHolder from the activity/fragment. The function returns null if no ViewHolder exists at given position

class YourAdapter(
    onCardClick: (Int) -> Unit
) {

    ...

    inner class YourViewHolder {
        fun bind() {
            cardView.setOnClickListener {
                onCardClick(adapterPosition)
            }
        }
      
        fun operateOnCard() {
            statusButtons.toggleVisibility(!statusButtons.isVisible)
        }
    }
}

class YourActivity {
    
    ...

    val adapter = YourAdapter { position ->
        val viewHolder = recyclerView.findViewHolderForLayoutPosition(position - 1) // Or whatever position you want
        (viewHolder as? YourViewHolder)?.operateOnCard()
    }
}

CodePudding user response:

Solution: in Adapter

// define flowState
(...)
 val _stateFlow = MutableStateFlow(-1)
 val stateFlow = _stateFlow.asStateFlow()

// after every change of element
(...)
                CoroutineScope(Dispatchers.Main).launch {
                    stateFlow.collect { id ->
                        statusButtons.toggleVisibility(item.id == id)
                    }
                }
  • Related