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.
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.
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();
}
});
}