SeekBar in a NavigationDrawer

前端 未结 2 757
花落未央
花落未央 2021-02-18 18:34

I want to use a seekbar in a Navigation Drawer

Basically I need to slide left and right to set the seekbar and, well, you guessed it, it slides the navigation drawer...<

相关标签:
2条回答
  • 2021-02-18 19:02

    Just add an onTouchListener. When you touch the screen on the seekbar (action_down) disallow parent to intercept the event.

    seekbar.setOnTouchListener(new ListView.OnTouchListener() 
    {
        @Override
        public boolean onTouch(View v, MotionEvent event) 
        {
            int action = event.getAction();
            switch (action) 
            {
            case MotionEvent.ACTION_DOWN:
                // Disallow Drawer to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(true);
                break;
    
            case MotionEvent.ACTION_UP:
                // Allow Drawer to intercept touch events.
                v.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            }
    
            // Handle seekbar touch events.
            v.onTouchEvent(event);
            return true;
        }
    });
    
    0 讨论(0)
  • 2021-02-18 19:10

    Use DrawerLayout.setDrawerLockMode

    instead of ViewParent.requestDisallowInterceptTouchEvent.

    In @dumazy answer requestDisallowInterceptTouchEvent solves this issue but it also creates another.

    While you are scrolling on navigation drawer, If you touch on SeekBar, OnSeekBarChangeListener will trigger. So scrolling event will break and seekbar progress will start to change by moving your finger.

    I modified the original answer a little bit to solve this issue using DrawerLockMode:

    mSeekBar.setOnTouchListener(new SeekBar.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
    
                case MotionEvent.ACTION_DOWN:
                    // Disallow Drawer to intercept touch events.
                    // v.getParent().requestDisallowInterceptTouchEvent(true);
                    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
                    break;
    
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    // Allow Drawer to intercept touch events.
                    // v.getParent().requestDisallowInterceptTouchEvent(false);
                    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNDEFINED);
                    break;
            }
    
            // Handle SeekBar touch events.
            v.onTouchEvent(event);
            return true;
        }
    });
    
    0 讨论(0)
提交回复
热议问题