RecyclerView 在一個設置BottomSheetBehavior的LinearLayout中,BottomSheet彈起時RecyclerView 無法滑動,通過重寫Touch監聽由RV的不同狀態進行事件消費處理滑動衝突
recyclerView.apply {
val linearLayoutManager = LinearLayoutManager(requireContext())
layoutManager = linearLayoutManager
adapter = rvAdapter
var touched = false
setOnTouchListener(View.OnTouchListener { v, event ->
if(linearLayoutManager.findFirstCompletelyVisibleItemPosition()==0 && touched){
//RV到頂部item完全顯示時將滑動交給父控件
v.parent.requestDisallowInterceptTouchEvent(false)
touched = false
return@OnTouchListener true
}
when (event.action) {
MotionEvent.ACTION_DOWN -> {
//不允許父控件攔截觸摸事件
v.parent.requestDisallowInterceptTouchEvent(true)
}
MotionEvent.ACTION_UP -> {
//允許父控件攔截觸摸事件
v.parent.requestDisallowInterceptTouchEvent(false)
touched = true
}
}
v.performClick()
v.onTouchEvent(event)
true
})
}