Android Navigation Drawer Doesn't Pass onTouchEvent to Activity

后端 未结 5 1068
忘掉有多难
忘掉有多难 2021-01-02 04:57

I have an Activity which uses the Android NavigationDrawer. When using only fragments (as usual), everything works perfect. But now I

5条回答
  •  佛祖请我去吃肉
    2021-01-02 05:22

    I have a solution:

    Set OnTouchListener on the screen layout (the first childview of DrawerLayout, normally) and transmit the TouchEvent to a custom GestureDetector.

    So, you can do your own things in it. One more important thing: if you want to override onSingleTapUp() or something else, you should return true in onDown() to make sure that you can get the rest MotionEvent to make onSingleTapUp() work.

    private class MyGestureListener implements GestureDetector.OnGestureListener{
        @Override
        public boolean onDown(MotionEvent e) {
    
            return true;
        }
    
        @Override
        public void onShowPress(MotionEvent e) {
    
        }
    
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            // do your own things
            return true;
        }
    
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return false;
        }
    
        @Override
        public void onLongPress(MotionEvent e) {
    
        }
    
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return false;
        }
    
    
    }
    

    and set it :

     mGestureDetector=new GestureDetector(this, new MyGestureListener());
    
        layout_content.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
    
                return  mGestureDetector.onTouchEvent(event);
            }
        });
    

提交回复
热议问题