使主要内容区域活跃在DrawerLayout主要内容、活跃、区域、DrawerLayout

2023-09-05 08:46:11 作者:圈子不同别硬融

如何使有源主要内容区域,而抽屉是打开的。目前,如果主内容区域被点击时,抽屉将被关闭,然后触摸事件将被丢弃,我要赶上触摸事件,并把它传递给主要内容的区域。

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.

推荐答案

您需要创建自定义DrawerLayout和@overide onInterceptTouchEvent 。然后你就可以创建一些方法,用于注册抽屉的意见(如 setDrawerViewWithoutIntercepting ),其将有可能使用(点击)主要内容,如果是开放的。你应该在你的活动/片段,这是哪里DrawerLayout用于调用此方法。

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将返回false以防万一注册抽屉打开视图和触摸的抽屉视图区域之外。这意味着所有的功能保持不变(如关闭使用刷卡抽屉视图等)

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;
}