android的RecyclerView点击事件

LinearLayout包含了多个组件,作为RecyclerView的item
我想实现点击任何一个组件,都触发同样的跳转事件,我发现点击在空白处才能正常跳转,被小组件覆盖的就不能触发点击事件!

下面这个解决方案我用了,并没达到我想要的效果,该怎么修改
https://blog.csdn.net/zjuter/article/details/118364344

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:leon="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:orientation="vertical">

    <!-- android:background="#00FFFFFF"-->
    <!--<View
        android:id="@+id/layoutEventArea"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         >

    </View>-->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/income_title"
            android:layout_width="match_parent"
            android:layout_height="32sp"

            android:clickable="true"
            android:gravity="center_vertical"
            android:paddingStart="20dp"
            android:text=""
            android:textColor="@color/black"
            android:textSize="18sp"

            tools:ignore="SpeakableTextPresentCheck" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:orientation="horizontal">


        <TextView
            android:id="@+id/income_detail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_weight="9"
            android:clickable="true"
            android:lineSpacingExtra="5dp"
            android:minHeight="50dp"
            android:paddingStart="20dp"
            android:text=""
            android:textColor="@color/drakgray"
            android:textSize="16sp"
            tools:ignore="SpeakableTextPresentCheck" />

        <ImageView
            android:id="@+id/income_see"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"


            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            app:srcCompat="@drawable/righticon" />
    </LinearLayout>


    <View
        android:id="@+id/divider3"
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:background="@color/smokegray" />

</LinearLayout>

以下回答参考GPT并且由Bony-整理:
您可以在LinearLayout中设置一个OnClickListener监听器,以便在点击该布局时触发相应的事件,而不仅仅是点击其中的某个组件。这样,当您点击LinearLayout中的任何一个组件时,都会触发相应的事件。请尝试以下更改:

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:orientation="vertical"
        android:onClick="onItemClick">

然后在您的Activity中添加以下方法:

public void onItemClick(View view) {
    // 触发相应的跳转事件
}

这样,当您点击LinearLayout时,会触发onItemClick()方法,您可以在该方法中编写相应的跳转逻辑。

  • 这篇博客: Android初学二之仿微信APP实现RecyclerView控件的设计开发,实现点击事件及图片瀑布流中的 3.3 实现点击事件对item中的每个LinearLayout实现点击显示内容 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • RecyclerView的点击事件有两种写法:
    (1)在Adapter里面直接对控件做点击事件;
    (2)写接口,在Activity或Fragment上实现接口中定义的方法。
    下面,我将采用第一种方法:
    在Adapter的函数onBindViewHolder()中添加点击事件代码

    final String content = mList.get(position);
            holder.itemView.setContentDescription(content);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, "您点击的联系人是:" + content, Toast.LENGTH_SHORT).show();
                }
            });
    

    在这里插入图片描述