码农-小林 2024-08-09 15:57 采纳率: 54.8%
浏览 55
已结题

RecyclerView类添加setOnTouchListener无效

RecyclerView类添加setOnTouchListener无效,添加其他类上可以
这是xml文件内容

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_45"
    android:layout_marginTop="@dimen/dp_20">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@color/color_79"
        android:textSize="@dimen/sp_17"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <!--图像巡检Layout-->
    <FrameLayout
        android:id="@+id/fl_icon"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:paddingHorizontal="@dimen/dp_15"
        app:layout_constraintRight_toRightOf="parent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:src="@mipmap/shaixuan" />

    </FrameLayout>

</android.support.constraint.ConstraintLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_0_5"
    android:background="@color/color_4c" />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="@dimen/dp_0"
    android:layout_marginLeft="@dimen/dp_0"
    android:layout_marginRight="@dimen/dp_0"
    android:background="@color/white"
    android:layout_marginHorizontal="@dimen/dp_0">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center"
        android:layout_marginTop="@dimen/dp_3"
        android:layout_marginLeft="@dimen/dp_0"
        android:layout_marginRight="@dimen/dp_0"
        android:background="@color/white"
        android:layout_marginHorizontal="@dimen/dp_1">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:orientation="vertical"
            android:layout_marginTop="@dimen/dp_0"
            android:layout_marginLeft="@dimen/dp_0"
            android:layout_marginRight="@dimen/dp_0"
            android:background="@color/white"
            android:layout_marginHorizontal="@dimen/dp_0">
            <TextView
                android:id="@+id/xj_txt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dp_6"
                android:layout_marginLeft="@dimen/dp_10"
                android:layout_marginRight="@dimen/dp_10"
                android:padding="@dimen/dp_0"
                android:textColor="@color/color_40"
                android:textSize="@dimen/sp_16"
                android:textStyle="bold"
                android:text=""/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_1"
                android:layout_marginTop="@dimen/dp_5"
                android:layout_marginLeft="@dimen/dp_10"
                android:layout_marginRight="@dimen/dp_10"
                android:padding="@dimen/dp_0"
                android:textColor="@color/color_40"
                android:textSize="@dimen/sp_12"
                android:textStyle="bold"
                android:text=""/>
        </LinearLayout>
        <ImageView
            android:id="@+id/pic_video_logo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="8"
            android:gravity="right"
            android:scaleType="fitCenter"
            android:layout_marginTop="@dimen/dp_10"
            android:layout_marginRight="@dimen/dp_16"
            android:layout_marginBottom="@dimen/dp_10"
            android:src="@mipmap/jiqiang"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="@dimen/dp_3"
        android:layout_marginLeft="@dimen/dp_0"
        android:layout_marginRight="@dimen/dp_0"
        android:background="@color/white"
        android:layout_marginHorizontal="@dimen/dp_1">

        <TextView
            android:id="@+id/tv_startdate"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="@dimen/dp_5"
            android:layout_marginTop="@dimen/dp_0"
            android:layout_marginRight="@dimen/dp_5"
            android:layout_weight="1"
            android:background="@drawable/fillet_device"
            android:gravity="center"
            android:padding="@dimen/dp_2"
            android:text="2022-10-21"
            android:textColor="@color/color_40"
            android:textSize="@dimen/sp_14" />
        <View
            android:layout_width="@dimen/dp_20"
            android:layout_height="@dimen/dp_1"
            android:layout_marginTop="30dp"
            android:background="@color/color_4c" />
        <TextView
            android:id="@+id/tv_enddate"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:layout_marginTop="@dimen/dp_2"
            android:layout_marginLeft="@dimen/dp_5"
            android:layout_marginRight="@dimen/dp_5"
            android:padding="@dimen/dp_2"
            android:gravity="center"
            android:background="@drawable/fillet_picture"
            android:textColor="@color/color_40"
            android:textSize="@dimen/sp_14"
            android:text="2022-10-21"/>

    </LinearLayout>
</LinearLayout>

<com.scwang.smartrefresh.layout.SmartRefreshLayout
    android:id="@+id/srl_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="afterDescendants"
    app:srlHeaderMaxDragRate="2"
    app:srlPrimaryColor="@android:color/darker_gray">

    <com.scwang.smartrefresh.layout.header.ClassicsHeader
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srlClassicsSpinnerStyle="Translate" />

    <android.support.v4.widget.NestedScrollView

        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">


            <android.support.v7.widget.RecyclerView
                android:id="@+id/lv_data"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginHorizontal="@dimen/dp_5"
                android:layout_marginTop="@dimen/dp_10" />

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <com.scwang.smartrefresh.layout.footer.ClassicsFooter
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srlClassicsSpinnerStyle="Translate" />

</com.scwang.smartrefresh.layout.SmartRefreshLayout>

img

  • 写回答

1条回答 默认 最新

  • 阿里小阿希 2024-08-09 16:01
    关注

    在Android开发中,如果你发现为RecyclerView设置setOnTouchListener无效,而其他视图上可以正常工作,这可能是因为RecyclerView内部的事件分发机制导致的。以下是一些可能的原因和解决方案:

    原因分析:

    1. 内部视图消费事件RecyclerView的子视图(如ViewHolder中的视图)可能已经消费了触摸事件。
    2. 事件拦截RecyclerView可能会拦截某些触摸事件,特别是滑动事件,以处理滚动。

      解决方案:

    3. **使用addOnItemTouchListener**:
      对于RecyclerView,你可以使用addOnItemTouchListener方法来添加触摸监听器,该方法专门用于处理RecyclerView条目的触摸事件。
      recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
          @Override
          public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
              // 处理触摸事件
              return false; // 如果返回true,则表示消费了事件,不会继续传递
          }
          @Override
          public void onTouchEvent(RecyclerView rv, MotionEvent e) {
              // 可以在这里处理触摸事件
          }
          @Override
          public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
              // 可以在这里处理是否允许父视图拦截触摸事件
          }
      });
      
    4. 为子视图设置触摸监听器
      如果需要在RecyclerView的子视图上处理触摸事件,可以在适配器中的onBindViewHolder方法中为子视图设置触摸监听器。
      holder.itemView.setOnTouchListener(new View.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
              // 处理触摸事件
              return true; // 返回true表示事件被消费,不会继续传递
          }
      });
      
    5. **自定义LayoutManager**:
      如果上述方法都不适用,你可以尝试自定义LayoutManager,并在其中处理触摸事件。
    6. **使用GestureDetector**:
      另一种方法是使用GestureDetector来检测特定的手势,并在RecyclerView的触摸监听器中实现。
      final GestureDetector gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
          @Override
          public boolean onSingleTapUp(MotionEvent e) {
              // 处理点击事件
              return true;
          }
      });
      recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
          @Override
          public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
              gestureDetector.onTouchEvent(e);
              return false;
          }
          @Override
          public void onTouchEvent(RecyclerView rv, MotionEvent e) {
              gestureDetector.onTouchEvent(e);
          }
          @Override
          public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
              // 可以在这里处理是否允许父视图拦截触摸事件
          }
      });
      
      通过上述方法,你应该能够解决RecyclerView添加setOnTouchListener无效的问题。如果问题仍然存在,你可能需要检查你的触摸监听器实现是否正确,或者是否有其他代码影响了触摸事件的传递。
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月17日
  • 已采纳回答 8月9日
  • 创建了问题 8月9日