make main content area active in DrawerLayout

2019-01-12 01:40发布

How to make active main content area, while drawer is open. Currently, if main content area is clicked, drawer will be closed then touch event will be dropped, I want to catch that touch event and pass it to main content area.

2条回答
Viruses.
2楼-- · 2019-01-12 01:50

I solved my problem with temporarily solution, not so elegant, but does the job for a while, maybe contains side effects. I always return false in onInterceptTouchEvent() call and everything is active everytime.

public class CustomDrawerLayout extends DrawerLayout {

    public CustomDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomDrawerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomDrawerLayout(Context context) {
        super(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // always false and event will be send to each view
        return false;
    }   
}
查看更多
神经病院院长
3楼-- · 2019-01-12 02:09

You need to create custom DrawerLayout and @overide onInterceptTouchEvent. Then you can create some method for registering drawer views (like setDrawerViewWithoutIntercepting), for which will be possible to use (click) main content, when are open. You should call this method in your activity/fragment, where is this DrawerLayout used.

OnInterceptTouchEvent will be returning false just in case registered drawer view is opened and touches are outside of drawer view area. This means all functionality remains the same (like closing drawer view using swiping, etc.)

This is just example which should be improved, so I hope it will help you.

private View currentDrawerView;

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    boolean result = super.onInterceptTouchEvent(event);

    if (currentDrawerView != null && isDrawerOpen(currentDrawerView)) {
        DrawerLayout.LayoutParams layoutParams = (DrawerLayout.LayoutParams) currentDrawerView.getLayoutParams();

        if (layoutParams.gravity == Gravity.RIGHT) {
            if (event.getX() < currentDrawerView.getX()) {
                result = false;
            }
        }
        else if (layoutParams.gravity == Gravity.LEFT) {
            if (event.getX() > currentDrawerView.getX() + currentDrawerView.getWidth()) {
                result = false;
            }
        }
        // ......

    };
    return result;
}

public void setDrawerViewWithoutIntercepting(View view) {
    this.currentDrawerView = view;
}
查看更多
登录 后发表回答