简体   繁体   中英

onClick() event at CardView

Is there any onClick() event at a CardView?

I didn't find any solution on the web.

Example:

<androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            app:cardCornerRadius="8dp"
            android:layout_marginRight="30dp"
            android:layout_marginTop="30dp"
            android:layout_marginLeft="30dp"
            android:layout_height="80dp"/>

So when I click on the cardview the layout_height goes up to 160dp. Is there any possibility?

You can set onClick() either programmatically or in the xml file. Here are them both ways:

In the xml file: (Make sure you have a method called changeHeight with a parameter of View view defined)

<androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            app:cardCornerRadius="8dp"
            android:layout_marginRight="30dp"
            android:layout_marginTop="30dp"
            android:layout_marginLeft="30dp"
            android:layout_height="80dp"
            android:onClick="changeHeight"
            android:id="cardview"/>

Notice that I set an id for the CardView here. You want to do this if you are going to set an onClickListener() programmatically.

Programmatically:

CardView cv = (CardView) findViewById(R.id.cardview); // using the id we set
// earlier.
cv.setOnClickListener(v -> {
    // set the height
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM