Android Navigation Drawer Doesn't Pass onTouchEvent to Activity

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

    To add on to guy_m 's answer, here is my implementation for a drawer that opens from the right, includes constructors so that it is viewable in the layout editor and also takes into account when a user swipes from past the edge point:

    public class CustomDrawerLayout extends DrawerLayout {
    View mDrawerListView;
    float edge;
    int holddown = 0;
    static final String TAG = CustomDrawerLayout.class.getSimpleName();
    
    public CustomDrawerLayout(@NonNull Context context) {
        super(context);
        setscreendimensionvals(context);
    }
    
    public CustomDrawerLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setscreendimensionvals(context);
    }
    
    public CustomDrawerLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setscreendimensionvals(context);
    }
    
    private void setscreendimensionvals(Context context){
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        /*((Activity) context).getWindowManager()
                .getDefaultDisplay()
                .getMetrics(displayMetrics); */
        int width = displayMetrics.widthPixels;
        float density = displayMetrics.density;
        edge = width - (30 * density); // 30 is the edge of the screen where the navigation drawer comes out
        Log.d(TAG,"edge: " + edge);
        Log.d(TAG,"width: " + width);
    }
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mDrawerListView = findViewById(R.id.drawerconstraint_overworld);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event){
        super.onTouchEvent(event); // need to add action up and a local variable to detect when lifted finger
        //Log.d(TAG,"point: " + event.getX());
        if(event.getX() >= edge && (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE)){
            holddown = 1;
            //Log.d(TAG,"hold down");
        }
    
        if(event.getAction() == MotionEvent.ACTION_UP){
            holddown = 0;
            //Log.d(TAG,"hold up");
        }
    
        if(holddown == 1){
            return  true;
        }else{
            if(isDrawerOpen(mDrawerListView) || isDrawerVisible(mDrawerListView)){
                return true;
            } else{
                return false;
            }
        }
    }
    

    }

提交回复
热议问题