kofoworola
kofoworola

Reputation: 557

How to know if view in recycler view item is clicked android

So i have been using recycler view with cardview in android and everything has been working fine...i just need to know how i can be alerted if a view in the list item is clicked not the entire list item it self.Here is my list item xml code

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
card_view:cardCornerRadius="5dp"
android:id="@+id/child_card"
android:layout_margin="5dp">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/child_name"
        android:background="@color/colorPrimaryLight"
        android:paddingLeft="5dp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/child_dob"
        android:layout_below="@+id/child_name"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textColor="#000000"
        android:paddingLeft="5dp"
        android:layout_marginTop="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/child_gender"
        android:textColor="#000000"
        android:paddingLeft="5dp"
        android:layout_below="@+id/child_dob"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="30dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/child_remove"
        android:layout_alignBottom="@+id/child_gender"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:src="@drawable/ic_clear_black_24dp"/>


</RelativeLayout>

what i need to be notified of on click is the child remove imageView. My recycler view has been initialized and is working fine.Thanks in advance

Upvotes: 0

Views: 459

Answers (1)

Chinmay Naphade
Chinmay Naphade

Reputation: 808

You could set an OnClickListener in the OnBind Method

 @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //your logic
            }
        });

    }

The other way to do it is you could implement OnClickListener in your ViewHolder class

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public ImageView image;

    public MyViewHolder(View itemView) {
        super(itemView);
        image = (ImageView) itemView.findViewById(R.id.child_remove);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v instanceOf ImageView){
          // your logic
        }
    }

}

Upvotes: 2

Related Questions