解决:
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>