SmartRefreshLayout icon indicating copy to clipboard operation
SmartRefreshLayout copied to clipboard

外层ViewPager2下,上拉加载更多造成ViewPager2误切的解决方案

Open KunPengZheng opened this issue 3 years ago • 0 comments

场景:SmartRefreshLayout在加载更多还没完成的情况下,继续上拉会造成ViewPager2切到其它position所处的页面

解决方案:在SmartRefreshLayout的直接父添加setOnTouchListener监听,伪代码如下: binding.cltRoot.setOnTouchListener( object : View.OnTouchListener {

            private var downX: Float = 0f
            private var downY: Float = 0f
            private val touchSlop = ViewConfiguration.get(context).scaledTouchSlop

            @SuppressLint("ClickableViewAccessibility")
            override fun onTouch(v: View?, event: MotionEvent?): Boolean {
                if (event == null) {
                    return false
                }

                when (event.action) {
                    MotionEvent.ACTION_DOWN -> {
                        downX = event.x
                        downY = event.y
                        return true
                    }
                    MotionEvent.ACTION_MOVE -> {
                        val dx = Math.abs(event.x - downX)
                        val dy = Math.abs(event.y - downY)
                        // 核心逻辑,只要判断方向是垂直方向,那么直接拦截,不透传给父级消耗。
                       // 如果你们的滑动方向是水平,那么依照下面进行改动即可
                        if (dy > touchSlop && dy > dx) {
                            Log.i("测试拦截触摸时间", "onTouch:111 ")
                            v?.parent?.requestDisallowInterceptTouchEvent(true)
                            return true
                        }
                    }
                }
                return false
            }
        })

总结:拦截事件,不透传给父级消耗。仅供思路参考,不一定适合其余人的项目

KunPengZheng avatar Jan 19 '22 12:01 KunPengZheng