OnClickListener on cardViews not working properly Kotlin

Issue

I’ve been having this problem for a couple of days, and I don’t see what I’m doing wrong.
I am trying to add a cardView with some Views inside, to a LinearLayout, one for each note that I have in the DB. So far no problem, the issue is that I also want to add an OnClickListener to each of those cardViews that I am creating programmatically. So that each time the CardView is clicked it expands or collapses.
The problem is that when doing so, all the CardViews share the OnClickListener, and when clicking on one, all the CardViews expand, I want only the one I’m clicking to expand.

I hope I have explained myself, I share part of the code.

for (nota in notas) {


                    val card = CardView(view.context)
                    val note = TextView(view.context)
                    val date = TextView(view.context)
                    val lLHor = LinearLayout(view.context)
                    val lLVer = LinearLayout(view.context)
                    val im = ImageView(view.context)

                    lLVer.layoutParams = LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT, 1f
                    )


                    with(im) {
                        setImageDrawable(
                            ContextCompat.getDrawable(
                                view.context,
                                R.drawable.ic_arrow_down
                            )
                        )
                        foregroundGravity = Gravity.END
                        layoutParams = LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT, 0f
                        )
                    }



                    lLHor.orientation = (LinearLayout.HORIZONTAL)
                    lLVer.orientation = (LinearLayout.VERTICAL)



                    with(note) {
                        setTextColor(Color.parseColor("#252323"))
                        text = nota.getString("Nota")
                        textSize = 20F
                        layoutParams = LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT, 1f
                        )
                    }


                    with(date) {
                        setTextColor(Color.parseColor("#252323"))
                        text = nota.getString("Fecha")
                        textSize = 20F
                        layoutParams = LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT, 0f
                        )
                    }


                    card.layoutParams = lP
                    card.setBackgroundColor(Color.parseColor("#FFFFFF"))

                    card.setOnClickListener {

                        cardEC(card, im)
                    }

                    lLVer.addView(date)
                    lLVer.addView(note)
                    lLHor.addView(lLVer)
                    lLHor.addView(im)
                    card.addView(lLHor)
                    linearNotas.addView(card)



                }

private fun cardEC(cardView: CardView, imageView: ImageView) {
    val height: Int =
        TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 28F, resources.displayMetrics)
            .toInt()

    val layPar = cardView.layoutParams

    if (cardView.layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
        layPar.height = height
        cardView.layoutParams = layPar
        imageView.setImageDrawable(
            view?.let {
                ContextCompat.getDrawable(
                    it.context,
                    R.drawable.ic_arrow_down
                )
            })
    } else {
        layPar.height = ViewGroup.LayoutParams.WRAP_CONTENT
        cardView.layoutParams = layPar
        imageView.setImageDrawable(
            view?.let {
                ContextCompat.getDrawable(
                    it.context,
                    R.drawable.ic_arrow_up
                )
            })

    }

}

Edit: I add images to see the problem well

Card before press

Then when i press any cardView

Cards after press

Solution

I "solved"
Although I still don’t understand why the problem occurred, I found a way to bypass it. I leave the solution I found in case it helps someone.
Based on the help @DavudDavudov gave me.
I keep the height of the CardView as WrapContent. When I create the TextView that contains the note, I set the View.Gone visibility to it. Then in the cardEC function I change the visibility of the TextView to View.Visible or View.Gone depending on how it was. And since the CardView is in WrapContent, plus the animateLayoutChanges It works perfectly, it gives the sensation of opening a drop-down menu. I copy part of the code in case it helps.

 with(note) {
                        setTextColor(Color.parseColor("#252323"))
                        text = nota.getString("Nota")
                        textSize = 20F
                        layoutParams = LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT, 1f
                        )
                        visibility = View.GONE
                    }
                    val noteContent = mutableListOf(note)
                    cardEC(card, im, noteContent)

private fun cardEC(cardView: CardView, imageView: ImageView, noteContent: MutableList<TextView>) {

    cardView.setOnClickListener {

        for (textView in noteContent){
            if (textView.visibility == View.VISIBLE){
                textView.visibility = View.GONE
                imageView.setImageDrawable(ResourcesCompat.getDrawable(resources, R.drawable.ic_arrow_down, null))
            }else{
                textView.visibility = View.VISIBLE
                imageView.setImageDrawable(ResourcesCompat.getDrawable(resources, R.drawable.ic_arrow_up, null))
            }
        }
    }

}

Answered By – Joel Delgado

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published