android - 防止 RecyclerView 在不创建自定义 ViewGroup 的情况下吞下触摸事件
问题描述
我目前正在开发一个看起来像这样
的 UI 蓝色部分是 ConstraintLayout 而紫色部分是其中的 RecyclerView(它是 RecyclerView,因为它的内容是基于服务响应的动态内容)。
我onClick
在 ConstraintLayout 上设置处理程序,将用户带到另一个页面。问题是 RecyclerView 正在消耗点击而不是将其转发给其父级。因此onClick
处理程序适用于蓝色区域,但不适用于紫色区域。
我尝试在 RecyclerView 中进行设置android:clickable="false"
,android:focusable="false"
但它仍然不会将点击传播到其父级。
我遇到的一种解决方案是从 ConstraintLayout 扩展并覆盖onInterceptTouchEvent()
以返回 true。但是我在我的项目中有一个严格的要求,不能创建自定义小部件,所以我不能使用这个解决方案。
有没有办法告诉 RecyclerView 停止消费触摸事件?
活动布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_margin="16dp"
android:background="#42d7f4"
android:onClick="navigate"
android:padding="16dp">
<TextView
android:id="@+id/headerText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="FRUITS"
android:textSize="36sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/itemsList"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#9f41f2"
android:clickable="false"
android:focusable="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
物品布局:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/itemText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:clickable="false"
android:focusable="false" />
活动.kt:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rcView = findViewById<RecyclerView>(R.id.itemsList)
rcView.layoutManager = LinearLayoutManager(this)
val items = listOf("Apple", "Banana", "Oranges", "Avocado")
rcView.adapter = ItemAdapter(items)
}
fun navigate(view: View) {
Toast.makeText(this, "Navigating to details page", Toast.LENGTH_SHORT)
.show()
}
}
class ItemAdapter(private val data: List<String>) : RecyclerView.Adapter<ItemViewHolder>() {
override fun getItemCount(): Int = data.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)
return ItemViewHolder(view)
}
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
holder.bind(data[position])
}
}
class ItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
private val itemTv: TextView = view.findViewById(R.id.itemText)
fun bind(item: String) {
itemTv.text = item
}
}
解决方案
如果在设置适配器后冻结布局,它不再吞下点击:
recyclerView.isLayoutFrozen = true // kotlin
recyclerView.setLayoutFrozen(true); // java
请记住,如果您需要更改适配器中的数据,则必须在调用 notifyDataSetChanged 之前解冻布局,然后重新冻结布局。我不喜欢这个解决方案,但它是唯一对我有用的解决方案。
推荐阅读
- php - Laravel 路由返回 404 (Laravel 6.6.2)
- electron - ElectronJS 应用程序无法在 ubuntu 20.04 上执行
- prolog - SWI PROLOG 中的家谱
- python - 如何在 python-flask 中使用 mysql.connector
- reactjs - 在material-ui React中处理多个MenuItem
- java - 有没有办法从测试中访问 Junit 配置参数
- libp2p - 使用 mdns 发现对等点的配置
- python - 如何网页抓取没有下一页
- android - 如何使用 MVVM 在 android studio Kotlin 中从 api 调用 google maps 标记的实时数据
- javascript - 访问嵌套数组属性