当前位置: 首页 > article >正文

[Android]NestedScrollView嵌套RecyclerView视图点击事件冲突问题

解决:

package com.mofsaas.www.ui.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.mofsaas.www.R
import com.mofsaas.www.utils.IconModel
import com.mofsaas.www.utils.NSLog

class HomeCategoryAdapter(private val items: List<IconModel>) : RecyclerView.Adapter<HomeCategoryAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val icon: ImageView = view.findViewById(R.id.item_home_fun_icon)
        val name: TextView = view.findViewById(R.id.item_home_fun_name)
        val but : Button = view.findViewById(R.id.item_home_fun_button)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_home_fun, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = items[position]
        holder.icon.setImageResource(item.icon)
        holder.name.text = item.name
        /// 设置事件监听,这里是为了处理NestedScrollView嵌套RecyclerView视图点击事件冲突问题,所以取消时ACTION_CANCEL也进行回调
        holder.but.setOnTouchListener { v, event ->
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    // 手指按下事件
                    NSLog("手指按下事件")
                    false // 返回 false 让事件继续传递给 setOnClickListener
                }
                MotionEvent.ACTION_UP -> {
                    // 手指抬起事件
                    NSLog("手指抬起事件")
                    item.click()
                    false // 返回 false 让事件继续传递给 setOnClickListener
                }
                MotionEvent.ACTION_CANCEL -> {
                    NSLog("手指事件取消")
                    item.click()
                    false // 返回 false 让事件继续传递
                }
                else -> {
                    NSLog("else event.action=${event.action}")
                    false
                }
            }
        }
    }

    override fun getItemCount(): Int = items.size
}

布局:

// 禁用 RecyclerView 的嵌套滚动
//home_fun_rv.isNestedScrollingEnabled = false
// 设置 LayoutManager
home_fun_rv.layoutManager = StaggeredGridLayoutManager(5, StaggeredGridLayoutManager.VERTICAL)
// 初始化适配器
cateAdapter = HomeCategoryAdapter(tempArr)
// 设置适配器
home_fun_rv.adapter = cateAdapter
<com.mofsaas.www.ui.view.LockableNestedScrollView
    android:id="@+id/home_tech_scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<com.mofsaas.www.ui.view.LockableRecyclerView
            android:id="@+id/home_fun_rv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layoutManager="androidx.recyclerview.widget.StaggeredGridLayoutManager"
            android:orientation="vertical"
            app:spanCount="4"
            tools:itemCount="4"
            tools:listitem="@layout/item_home_fun" />
            
  </com.mofsaas.www.ui.view.LockableNestedScrollView>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/linear_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_click"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:padding="10dp"
        android:clickable="false"
        android:focusable="false"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <ImageView
            android:id="@+id/item_home_fun_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:clickable="false"
            android:focusable="false"
            tools:src="@mipmap/icon_home_fun_charge" />

        <TextView
            android:id="@+id/item_home_fun_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:textColor="@color/color_normal"
            android:textSize="12sp"
            android:clickable="false"
            android:focusable="false"
            tools:text="会员充值" />

    </androidx.appcompat.widget.LinearLayoutCompat>

    <!-- 添加一个透明按钮覆盖整个 LinearLayoutCompat -->
    <Button
        android:id="@+id/item_home_fun_button"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:focusable="true"
        android:visibility="visible"
        app:layout_constraintTop_toTopOf="@id/linear_layout"
        app:layout_constraintBottom_toBottomOf="@id/linear_layout"
        app:layout_constraintStart_toStartOf="@id/linear_layout"
        app:layout_constraintEnd_toEndOf="@id/linear_layout" />
</androidx.constraintlayout.widget.ConstraintLayout>


http://www.kler.cn/a/555611.html

相关文章:

  • 【AI】GitHub Copilot
  • AF3 _realign_pdb_template_to_query 函数解读
  • DeepSeek 冲击(含本地化部署实践)
  • ios UICollectionView使用
  • android调用ffmpeg解析rtsp协议的视频流
  • 【重磅发布】OmniParser V2-开启GUI自动化新时代
  • Spark(2)linux和简单命令
  • PDF文档管理系统V2.0
  • Mac端homebrew安装配置
  • 安全防御综合练习2 nat+智能选路
  • 问题记录汇总
  • ref() 和 reactive()响应性 浅解
  • mapbox基础,使用geojson加载Fill面图层
  • docker之图形界面portainer.io无法访问问题及解决办法
  • 挖掘图片的秘密:如何用piexif提取和修改Exif数据
  • 正则表达式–断言
  • 蓝桥杯班级活动
  • javaEE-13.spring MVC
  • k8s的资源说明 cpu 内存 requests usage limits
  • freesiwtch启用esl,nodejs调用api