简体   繁体   中英

How to darken CardView using onClick?

This is my first time asking a question here, so apologies if I do something wrong.

I have been using this playlist to make a RecyclerView in my app, with individual CardViews. I want to make it so that when a cardview is clicked, it will change the card to a slightly dark gray, to indicate that it was clicked. However, I don't know how to go about doing this. If someone could help me, that would be greatly appreciated. Let me know if you need any more info.

On onclick() method you just have to change card background color. You can do this by this command.

card.setCardBackgroundColor(color);

On the setOnClickListener method, you have to add

card.setCardBackgroundColor(colorYouWant);

and you can add it directly to the onBindViewHolder in the adapter class of the RecycleView.

Hope it works for you!

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