【发布时间】:2019-07-22 23:10:39
【问题描述】:
我目前正在开发一个看起来像这样的用户界面
蓝色部分是 ConstraintLayout 而紫色部分是其中的 RecyclerView(它是 RecyclerView,因为它的内容是基于服务响应动态的)。
我在 ConstraintLayout 上设置了onClick 处理程序,它将把用户带到另一个页面。问题是 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" />
Activity.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本身可以点击吗?换句话说,为ConstraintLayout和RecyclerView设置了一个点击处理程序。 -
尝试在RecyclerView上设置,好像不行。
-
您是否要滚动
recyclerview? @Abhishek -
@Abhishek 你想滚动 recyclerview 但不想点击?
-
如果
RecyclerView不需要滚动,我会按照@TanveerMunir 的建议使用叠加层,或者您可以在活动中使用dispatchTouchEvent()。
标签: android android-recyclerview androidx