【问题标题】:Clicking CardView instead of Clicking Items Inside单击 CardView 而不是单击里面的项目
【发布时间】:2017-01-13 10:36:50
【问题描述】:

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

Whatsapp Multiselect

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

【问题讨论】:

    标签: android onclick android-recyclerview multi-select cardview


    【解决方案1】:

    CardView 也是一个可以在 CardView 上设置 View.OnClickListener 的 View。

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

    【讨论】:

    • 感谢 Shahzaib 的回答,我已经在使用 CardView OnClick。问题是,我可以在一些没有子项目的地方使用它。当有子项时,我无法单击卡片视图,而是触发子项的 OnClick。我尝试将子点击设置为 null 并将它们的 setEnabled 设置为 false,但我仍然无法在这些子项目上实现 CardView Click。
    • 然后您可以尝试其他方法..您可以在 CardView 中创建一个视图并使其与父级匹配..然后在该视图上使用 onClick ..我会在一段时间内编辑答案希望这会有所帮助你
    • 好的,我们读的是同一所大学。期待Comsian的回复!
    • 抱歉回复晚了。我现在是第四学期的学生,正在准备期末考试:)
    • 感谢您的回答。我比你高。如果您在 FYP 等方面需要任何帮助,我很乐意为您提供帮助。
    猜你喜欢
    • 2021-11-12
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多