美文网首页
解决SwipeRefreshLayout和ViewPager2滑

解决SwipeRefreshLayout和ViewPager2滑

作者: 菜鸟考官 | 来源:发表于2021-09-08 09:01 被阅读0次
    class SwipeRefreshLayoutViewPager2 : SmartRefreshLayout {
    constructor(context: Context, attes: AttributeSet) : super(context, attes) {}
    constructor(context: Context) : super(context)
    
    private var startX = 0
    private var beginScrolll = false //是否开始滑动
    private var startY: Int = 0
    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        when (ev.action) {
            MotionEvent.ACTION_DOWN -> {
                startX = ev.x.toInt()
                startY = ev.y.toInt()
                parent.requestDisallowInterceptTouchEvent(true)
            }
            MotionEvent.ACTION_MOVE -> {
                val endX = ev.x.toInt()
                val endY = ev.y.toInt()
                val disX = Math.abs(endX - startX)
                val disY: Int = Math.abs(endY - startY)
                if (disX > disY) {
                    if (!beginScrolll)
                        parent.requestDisallowInterceptTouchEvent(false)
                } else {
                    beginScrolll = true
                    parent.requestDisallowInterceptTouchEvent(true)
                }
            }
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                parent.requestDisallowInterceptTouchEvent(false)
                beginScrolll=false
            }
        }
        return super.dispatchTouchEvent(ev)
    }
    

    }

    相关文章

      网友评论

          本文标题:解决SwipeRefreshLayout和ViewPager2滑

          本文链接:https://www.haomeiwen.com/subject/ueamwltx.html