Android Navigation Drawer Doesn't Pass onTouchEvent to Activity

后端 未结 5 1054
忘掉有多难
忘掉有多难 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:14

    With these answers, i still had some trouble. I could get the motionEvent back to the activity but I lost the onClick listener answer by fragment or everything on the screen. So I found another way to have everything work ( get answer when override OntouchEvent from activity, and answer to onClick Listener )

    Extend DrawerLayout and Override this methode :

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(super.onInterceptTouchEvent(ev)) return true;
        else {
            Activity activity = AppContext.getCurrentActivity();
            return activity.onTouchEvent(ev);
        }
    }
    

    if the drawer want the motion event, let it handle it. And if not, pass the event to activity yourself. (AppContext.getCurrentActivity is something from you with current activity, you can for instance attach activity as weakreference to the drawerLayout OnCreate)

    The good thing with this way, you don't care about the edge and don't care if start or end. And you don't care also if it is open or close. Everything work fine.

提交回复
热议问题