单击 CardView 而不是单击内部项目

Clicking CardView instead of Clicking Items Inside

我正在使用 RecyclerView 和 CardView。我正在关注 UI 之类的 WhatsApp。当 Whatsapp 用户长按主屏幕聊天选项卡中的联系人时,用户可以同时 select 多个联系人。

Whatsapp Multiselect

我希望用户像在 whatsapp 屏幕中一样,通过单击卡片视图中的任意位置来进行多select。我被卡在回收站视图内的 CardView 的点击中。我只希望 cardview 的 onclick,内部项目的其余点击不可点击,这样当用户是 multiselecting 时它们就不会干扰。将不胜感激解决此问题的任何帮助。

CardView 也是一个视图,您可以在 CardView 上设置 View.OnClickListener。

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

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

并且在你的 onBindViewHolder()

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

或者你可以这样做。

卡片视图在 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>

并且在您的 RecyclerView 适配器中

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

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

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