2

I am using RecyclerView and CardView. I am following the WhatsApp like UI. When the Whatsapp user long presses the Contacts in the Chat Tab of Home Screen it enables the users to select multiple Contacts at the same time.

Whatsapp Multiselect

I want the user to multiselect by clicking anywhere in the cardview just like in whatsapp screen. I am stuck at the clicking of the CardView inside recycler view. I want only the onclick of cardview, rest of the clicks of items inside to be not clickable so that they dont interfere when user is multiselecting. Any help in resolving this issue will be greatly appreciated.

topgun
  • 43
  • 1
  • 10

1 Answers1

1

CardView is also a View you can set View.OnClickListener on CardView.

public class MyViewHolder extends RecyclerView.ViewHolder {
    public View view;

    public MyViewHolder(View view) {
        super(view);
        this.view = view;
    }
}

And in your onBindViewHolder()

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    holder.view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Your Logic
        }
    });
}

Or You can do this.

CardView in xml

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_margin="@dimen/card_margin"
    android:elevation="3dp"
    card_view:cardCornerRadius="@dimen/card_album_radius">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_margin="7dp"
            android:id="@+id/thumbnail"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_centerHorizontal="true"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:clickable="true"
            android:scaleType="fitXY"/>

        <TextView
            android:id="@+id/channel_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/thumbnail"
            android:paddingLeft="@dimen/album_title_padding"
            android:paddingRight="@dimen/album_title_padding"
            android:paddingTop="@dimen/album_title_padding"
            android:textColor="#4c4c4c"
            android:textSize="16dp"
            android:text="Ary News"
            android:fontFamily="sans-serif-smallcaps"
            android:gravity="center_horizontal"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Watch Now"
            android:layout_below="@id/channel_name"
            android:fontFamily="sans-serif-smallcaps"
            android:id="@+id/watch_now"
            android:textStyle="bold"
            android:backgroundTint="@color/colorAccent"/>

        <!--This is the view-->

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/view"/>

    </RelativeLayout>

</android.support.v7.widget.CardView>

And in your RecyclerView Adapter

public class MyViewHolder extends RecyclerView.ViewHolder {
public View view;

public MyViewHolder(View view) {
    super(view);
    this.view = findViewById(R.id.view);
}}

In onBindViewHolder()

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    holder.view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        Toast.makeText(context,"Card clicked",Toast.LENGTH_SHORT).show();
        }
    });
}
  • 1
    Thanks Shahzaib for the answer, I am already using CardView OnClick. Problem is, I am able to use it in some places where there are no child items. When there are child Items I cannot click the Card View and the OnClick of the child items get triggered instead. I tried putting child clicks to null and their setEnabled as false, still I cannot achieve the CardView Click over those child items. – topgun Jan 14 '17 at 10:22
  • Then you can try something else.. You can make a view in CardView and make it match parent.. Then use onClick on that view.. I'll edit answer in a while hope this would help you – Rana Shahzaib Jan 14 '17 at 10:27
  • 1
    Okay, We have studied the same university. Looking forward for the reply Comsian! – topgun Jan 14 '17 at 12:32
  • Sorry for the late reply. I am currently student here of 4th semester and was preparing for my final exams :) – Rana Shahzaib Jan 14 '17 at 13:28
  • Thanks for the answer. I am quite senior to you. I would gladly help you in case you need any assistance in your FYP etc.. – topgun Jan 14 '17 at 14:19