OnClickListener for CardView?
You should implement the OnItemClickListener
in your ViewHolder
class, and pass the current item to the ViewHolder
instances on every onBindViewHolder()
.
From this post:
public static class ViewHolder extends RecyclerView.ViewHolder {
public View view;
public Item currentItem;
public ViewHolder(View v) {
super(v);
view = v;
view.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
// item clicked
}
});
}
}
@Override public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.currentItem = items.get(i);
}
Adding onclicklistener on cardview
When you call getIntent(), you are getting de reference of intent used to call your current activity.
Inside your switch case, you have a instantiation of new Intent that is not been used.
So, the solution is:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.draftCard:
Intent i = new Intent(this, draftcard.class);
startActivity(i);
break;
case R.id.cardsCard:
Intent i = new Intent(this, cardscard.class);
startActivity(i);
break;
case R.id.moneyCard:
Intent i = new Intent(this, moneycard.class);
startActivity(i);
break;
case R.id.todoCard:
Intent i = new Intent(this, todocard.class);
startActivity(i);
break;
case R.id.linkedinCard:
Intent i = new Intent(this, linkedincard.class);
startActivity(i);
break;
default:break;
}
}
OnClickListener on cardViews not working properly Kotlin
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))
}
}
}
}
Recyclerview + CardView OnClickListener
You can add the OnClickListener
to the root item of the ReyclerView
row which is itemView
in your example.
You can do that optionally in the ViewHolder
constructor or in onBindViewHolder
method
Here it's in the ViewHolder
public class ViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView gridIcon;
public ViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.text_1);
gridIcon = itemView.findViewById(R.id.image_1);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "My Item position: " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
});
}
}
And in the onBindViewHolder
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.title.setText(titles.get(position));
holder.gridIcon.setImageResource(images.get(position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "My Item position: " + position, Toast.LENGTH_SHORT).show();
}
});
}
Related Topics
How to Use Android Canvas to Draw a Rectangle with Only Topleft and Topright Corners Round
How to Iterate Through a View's Elements
Can an Aar Include Transitive Dependencies
Android Alternate Row Colors in Listview
Fast Ways to Import Drawables in Android Studio
How to Dynamically Query the Room Database at Runtime
Android Listview Get Selected Item
Stop Saving Photos Using Android Native Camera
How to Change Font Size in Preferencescreen
Adding a Color Filter to a Drawable Changes All Buttons Using the Same Drawable
How to Add a Switch to Android Action Bar
Android: How to Create a Motionevent
Android: How Does Gridview Auto_Fit Find the Number of Columns
Send a Notification When the App Is Closed
How to Play Audio File in Android
Firebase Cloud Messaging - How to Validate Tokens