首页 文章

单击CardView而不是单击里面的项目

提问于
浏览
1

我正在使用RecyclerView和CardView . 我正在关注像WhatsApp这样的UI . 当Whatsapp用户长按主屏幕的聊天选项卡中的联系人时,它允许用户同时选择多个联系人 .

Whatsapp Multiselect

我希望用户通过点击cardview中的任意位置进行多选,就像在whatsapp屏幕中一样 . 我被困在了回收器视图里面的CardView点击 . 我只想点击cardview,其中内部项目的其余部分不可点击,这样当用户多选时它们就不会干扰 . 任何帮助解决这个问题将不胜感激 .

1 回答

  • 0

    CardView也是一个View,您可以在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
            }
        });
    }
    

    或者你可以这样做 .

    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>
    

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

相关问题