kotlin語言:解決drawerLayout與viewpager的衝突、NavigationView側滑裡面menu的點選事件

weixin_41650019發表於2020-10-29

一、需求
1.解決滑動衝突:
第一頁的時候,把viewpager的向右滑動禁止掉,這樣drawlayout就能自然滑出來;
當drawerlayout滑出後,禁止viewpager滑動,關閉drawerlayout後,開啟viewpager滑動
2.navigationview中側滑menu的點選事件
二、實現步驟

滑動衝突的解決

1.重寫viewpager

class MyViewPager : ViewPager {
    private var xDistance = 0f
    private var xLast = 0f
    private var noScroll = false

    constructor(context: Context?) : super(context!!) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {}

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        if (noScroll) {
            return false
        }
        when (ev.action) {
            MotionEvent.ACTION_DOWN -> {
                xDistance = 0f
                xLast = ev.x
            }
            MotionEvent.ACTION_MOVE -> {
                val curX = ev.x
                if (xLast - curX < 0 && currentItem == 0) {
                    return false
                }
            }
        }
        return super.onInterceptTouchEvent(ev)
    }

    override fun scrollTo(x: Int, y: Int) {
        super.scrollTo(x, y)
    }
    fun setNoScroll(noScroll: Boolean) {
        this.noScroll = noScroll
    }

    override fun onTouchEvent(arg0: MotionEvent): Boolean {

        return if (noScroll) false else super.onTouchEvent(arg0)
    }

    override fun setCurrentItem(item: Int, smoothScroll: Boolean) {
        super.setCurrentItem(item, smoothScroll)
    }

    override fun setCurrentItem(item: Int) {
        super.setCurrentItem(item)
    }
}

2.監聽drawerlayout的滑動

        drawer_layout_main.setDrawerListener(object : DrawerLayout.DrawerListener {
            override fun onDrawerSlide(drawerView: View, slideOffset: Float) {}
            override fun onDrawerOpened(drawerView: View) {
                //禁止Viewpager的滑動,從而避免與DrawerLayout的衝突
                main_viewpager.setNoScroll(true)
            }

            override fun onDrawerClosed(drawerView: View) {
                //解除Viewpager的禁止滑動
                main_viewpager.setNoScroll(false)
            }

            override fun onDrawerStateChanged(newState: Int) {}
        })

參考文章:
解決滑動衝突

側滑menu的點選事件

        nav_view.setNavigationItemSelectedListener(object :
            NavigationView.OnNavigationItemSelectedListener {
            override fun onNavigationItemSelected(item: MenuItem): Boolean {
                when (item.itemId) {
                    R.id.nav_main -> {
                        main_viewpager.currentItem = 0
                        drawer_layout_main.closeDrawer(Gravity.LEFT)
                    }
                    R.id.nav_type -> {
                        main_viewpager.currentItem = 1
                        drawer_layout_main.closeDrawer(Gravity.LEFT)
                    }
                    R.id.nav_history -> {
                        main_viewpager.currentItem = 2
                        drawer_layout_main.closeDrawer(Gravity.LEFT)
                    }
                    R.id.nav_setting -> {
                        main_viewpager.currentItem = 3
                        drawer_layout_main.closeDrawer(Gravity.LEFT)
                    }
                }
                return true
            }
        })

相關文章