Android 防止RecyclerView在不创建自定义视图组的情况下吞咽触摸事件

Android 防止RecyclerView在不创建自定义视图组的情况下吞咽触摸事件,android,android-recyclerview,androidx,Android,Android Recyclerview,Androidx,我目前正在开发一个类似这样的UI 蓝色部分是ConstraintLayout,而紫色部分是其中的RecyclerView(它是RecyclerView,因为它的内容是基于服务响应的动态内容) 我正在ConstraintLayout上设置onClick处理程序,它将用户带到另一个页面。问题是RecyclerView正在使用这些点击,而不是将其转发给其父级。因此,onClick处理程序适用于蓝色区域,但不适用于紫色区域 我尝试在RecyclerView中设置android:clickable=“f

我目前正在开发一个类似这样的UI

蓝色部分是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
    }
}
class MainActivity:AppCompatActivity(){
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rcView=findviewbyd(R.id.itemsList)
rcView.layoutManager=LinearLayoutManager(此)
val项目=列表(“苹果”、“香蕉”、“橙子”、“鳄梨”)
rcView.adapter=ItemAdapter(项目)
}
趣味导航(视图:视图){
Toast.makeText(这是“导航到详细信息页面”,Toast.LENGTH\u SHORT)
.show()
}
}
类ItemAdapter(私有val数据:列表):RecyclerView.Adapter(){
重写getItemCount():Int=data.size
重写CreateViewHolder(父级:ViewGroup,viewType:Int):ItemViewHolder{
val view=LayoutInflater.from(parent.context).充气(R.layout.item,parent,false)
返回项目视图持有者(视图)
}
覆盖BindViewHolder(holder:ItemViewHolder,位置:Int){
holder.bind(数据[位置])
}
}
类ItemViewHolder(视图:视图):RecyclerView.ViewHolder(视图){
private val itemTv:TextView=view.findviewbyd(R.id.itemText)
趣味绑定(项目:字符串){
itemTv.text=项目
}
}

您可以将
回收视图
焦点设置为false

recyclerView.setFocusable(false);
和/或设置其行
view.setFocusable(false)

编辑

如果要为
ConstraintLayout
提供焦点

layout.setFocusable(true);
layout.setClickable(true);

//
// add click listener and event ....
// 
有关更多信息,请参阅谷歌提供的


希望这会有所帮助,干杯可能要完全阻止与单个视图中任何内容的交互,最简单的方法是在其上放置一个透明的视图,以拦截所有触摸事件。您可以设置单击该视图,并通过该视图管理所有功能

比如像这样

<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: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" />
            <View
                android:id="@+id/clickView"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:onClick="navigate"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:clickable="true"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </FrameLayout>


一种方法是使用click listener设置
RecyclerView
行,并禁用对
RecyclerView
行的子项的单击和长时间单击,如下所示:

ConstraintLayout: `android:onClick="navigate"`
   For the item layout: `android:onClick="navigate"`
       TextView: android:clickable="false"
                 android:longClickable="false"
        (etc. for all children of the row)

我认为缺少的是使
文本视图不可长时间单击。

如果在设置适配器后冻结布局,它将不再接受单击:

recyclerView.isLayoutFrozen = true // kotlin
recyclerView.setLayoutFrozen(true); // java

请记住,如果需要更改适配器中的数据,则必须在调用notifyDataSetChanged之前解冻布局,然后重新冻结布局。我不喜欢这个解决方案,但它是唯一适合我的解决方案。

我尝试将focusable和clickable设置为false(在xml和fragment中)。ConstraintLayout仍无法获取click事件。那么您是否已将焦点赋予ConstraintLayout?ConstraintLayout将在除RecyclerView之外的所有部分中获取焦点。我在我的帖子中添加了代码示例。你不能让
RecyclerView
本身可以点击吗?换言之,为
ConstraintLayout
RecyclerView
设置了一个单击处理程序。尝试将其设置为RecyclerView时,似乎不起作用。是否要滚动
RecyclerView
@Abhishek@Abhishek您想滚动recyclerview,但不想单击?如果
recyclerview
不需要滚动,我会按照@TanveerMunir的建议使用覆盖,或者您可以在活动中使用。