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

news/2025/2/22 3:29:46

解决:

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.niftyadmin.cn/n/5861559.html

相关文章

网工项目实践2.4 北京公司安全加固、服务需求分析及方案制定

本专栏持续更新&#xff0c;整一个专栏为一个大型复杂网络工程项目。阅读本文章之前务必先看《本专栏必读》。 全网拓扑图展示 一.局域网规划设计 1.子公司北京总部局域网安全加固、网络服务需求 子公司北京总部在与运营商边界需要部署一台防火墙&#xff0c;保护内网的安全。…

RoboBERT:减少大规模数据与训练成本,端到端多模态机器人操作模型(西湖大学最新)

写在前面&出发点 具身智能融合多种模态&#xff0c;使智能体能够同时理解图像、语言和动作。然而&#xff0c;现有模型通常依赖额外数据集或大量预训练来最大化性能提升&#xff0c;这耗费了大量训练时间和高昂的硬件成本。为解决这一问题&#xff0c;我们提出RoboBERT&…

DeepSeek 助力 Vue 开发:打造丝滑的复制到剪贴板(Copy to Clipboard)

前言&#xff1a;哈喽&#xff0c;大家好&#xff0c;今天给大家分享一篇文章&#xff01;并提供具体代码帮助大家深入理解&#xff0c;彻底掌握&#xff01;创作不易&#xff0c;如果能帮助到大家或者给大家一些灵感和启发&#xff0c;欢迎收藏关注哦 &#x1f495; 目录 Deep…

250217-数据结构

1. 定义 数据结构是数据的存储结构&#xff0c;即数据是按某些结构来存储的&#xff0c;比如线性结构&#xff0c;比如树状结构等。 2. 学习意义 数据结构是服务于算法的&#xff0c;为了实现算法的高效计算&#xff0c;所以将数据按特定结构存储。比如使用快速插入或删除的…

OpenSSL has been compiled without RC2 support

记性不好&#xff0c;以此记录日常遇到的问题 问题 原因 研究发现linux的OpenSSL版本过高&#xff0c;已经抛弃了RC2 方法 通过conda进行安装openssl&#xff0c;指定版本 conda install openssl1.1.1s 于此同时&#xff0c;如果你之前安装了uwsgi&#xff0c;会发现uwsgi会…

【linux】更换ollama的deepseek模型默认安装路径

【linux】更换ollama的deepseek模型默认安装路径 文章目录 【linux】更换ollama的deepseek模型默认安装路径Ollama 默认安装路径及模型存储路径迁移ollama模型到新的路径1.创建新的模型存储目录2.停止ollama3.迁移现有模型4.修改 Ollama 服务配置5.重启ollama6.验证迁移是否成功…

springboot404-基于Java的校园礼服租赁系统(源码+数据库+纯前后端分离+部署讲解等)

&#x1f495;&#x1f495;作者&#xff1a; 爱笑学姐 &#x1f495;&#x1f495;个人简介&#xff1a;十年Java&#xff0c;Python美女程序员一枚&#xff0c;精通计算机专业前后端各类框架。 &#x1f495;&#x1f495;各类成品Java毕设 。javaweb&#xff0c;ssm&#xf…

改BUG:远程连接redis失败,可能是防火墙的问题

问题再现 import redis# 假设虚拟机的 IP 地址为 192.168.XX.XX&#xff0c;Redis 默认端口为 6379 r redis.StrictRedis(host192.168.171.135, port6379, db0, password123456)try:# 测试连接r.ping()print("成功连接到虚拟机中的 Redis&#xff01;") except red…