Android BottomSheetBehavior RecyclerView 滑動衝突解決

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
            })
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章