重叠视图间RecyclerView与Bottom Sheet拖拽异常问题排查
问题场景
实现两个RecyclerView间的条目拖拽功能,其中一个RecyclerView嵌套在Bottom Sheet内。当Bottom Sheet与目标RecyclerView处于同一LinearLayout中时,拖拽功能正常运行;但改用RelativeLayout作为父布局,让Bottom Sheet覆盖主内容区域后,onDrag()事件无法在Bottom Sheet外部触发,拖拽功能完全失效。
正常工作的布局代码:
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:orientation="vertical" app:layout_constraintBaseline_toTopOf="parent" android:gravity="center_horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="25sp" android:paddingTop="20dp" android:paddingBottom="10dp" android:text="Target RecyclerView" /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rvTop" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="16dp" android:paddingBottom="16dp"/> <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/bottom_sheet_dialog" /> </androidx.coordinatorlayout.widget.CoordinatorLayout> </LinearLayout>
失效的布局代码:
<RelativeLayout 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"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:orientation="vertical" app:layout_constraintBaseline_toTopOf="parent" android:gravity="center_horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="25sp" android:paddingTop="20dp" android:paddingBottom="10dp" android:text="Target RecyclerView" /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rvTop" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="16dp" android:paddingBottom="16dp"/> </LinearLayout> <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/bottom_sheet_dialog" /> </androidx.coordinatorlayout.widget.CoordinatorLayout> </RelativeLayout>
问题根源
Bottom Sheet依赖的CoordinatorLayout本质是增强版FrameLayout,它会默认拦截并消费触摸/拖拽事件,导致事件无法传递到下层的目标RecyclerView,进而触发不了外部视图的onDrag()回调。
优化解决方案
方案1:自定义CoordinatorLayout,允许拖拽期间事件透传
自定义一个CoordinatorLayout,通过重写事件拦截方法,在拖拽状态下不拦截事件,让事件能传递到下层视图:
class PassthroughCoordinatorLayout @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : CoordinatorLayout(context, attrs, defStyleAttr) { private var isDraggingItem = false // 对外暴露方法,标记是否处于拖拽状态 fun setDraggingItem(dragging: Boolean) { isDraggingItem = dragging } override fun onInterceptTouchEvent(ev: MotionEvent): Boolean { // 拖拽期间不拦截事件,交给下层视图处理 return if (isDraggingItem) false else super.onInterceptTouchEvent(ev) } override fun onTouchEvent(ev: MotionEvent): Boolean { // 拖拽期间不消费事件,让事件继续传递 return if (isDraggingItem) false else super.onTouchEvent(ev) } }
使用时,将布局中的CoordinatorLayout替换为这个自定义类,在拖拽开始时调用setDraggingItem(true),拖拽结束后调用setDraggingItem(false)即可。
方案2:调整布局层级,让目标RecyclerView处于上层
利用RelativeLayout的层级特性(后添加的视图层级更高),把目标RecyclerView所在的LinearLayout放在CoordinatorLayout之后,这样拖拽事件会先被目标RecyclerView接收:
<RelativeLayout 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"> <!-- 先放CoordinatorLayout,层级更低 --> <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/bottom_sheet_dialog" /> </androidx.coordinatorlayout.widget.CoordinatorLayout> <!-- 后放目标RecyclerView所在布局,层级更高 --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:orientation="vertical" android:gravity="center_horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="25sp" android:paddingTop="20dp" android:paddingBottom="10dp" android:text="Target RecyclerView" /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rvTop" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="16dp" android:paddingBottom="16dp"/> </LinearLayout> </RelativeLayout>
注意:如果Bottom Sheet需要正常交互,要确保在非拖拽状态下,Bottom Sheet的内容区域能被触摸到,可通过调整视图的clickable或focusable属性实现。
方案3:在Activity中手动转发拖拽事件
重写Activity的dispatchTouchEvent方法,在拖拽期间将事件手动分发给目标RecyclerView:
private var isDraggingItem = false override fun dispatchTouchEvent(ev: MotionEvent): Boolean { val rvTop = findViewById<RecyclerView>(R.id.rvTop) if (isDraggingItem && rvTop.isShown) { // 将屏幕坐标转换为RecyclerView的局部坐标 val location = IntArray(2) rvTop.getLocationOnScreen(location) ev.offsetLocation(-location[0].toFloat(), -location[1].toFloat()) // 转发事件给目标RecyclerView rvTop.dispatchTouchEvent(ev) } return super.dispatchTouchEvent(ev) }
同样需要在拖拽开始和结束时更新isDraggingItem的状态。
方案对比
- 方案1:灵活性最高,不改动原有布局结构,适合需要同时保留Bottom Sheet交互和拖拽功能的复杂场景
- 方案2:实现成本最低,仅需调整布局顺序,适合Bottom Sheet仅作为数据源容器、不需要频繁交互的场景
- 方案3:代码侵入性稍高,适合无法自定义CoordinatorLayout或调整布局的特殊场景
内容的提问来源于stack exchange,提问作者Tomás Crespo

