当且仅当我单击按钮时,我想关闭导航抽屉

I want to Close Navigation Drawer if and only if I click the Button

我使用下面的代码创建了导航抽屉。

activity_inventory.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#80000000"
        android:visibility="gone" />

    <com.example.softeng.animationf.fabdirectory.ActionButton
        android:id="@+id/fab_activity_action_button"
        style="@style/fab_action_button_style"
        android:layout_gravity="bottom|right"
        fab:type="MINI"/>


</RelativeLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="end">

    <RelativeLayout
        android:id="@+id/right_navigation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#4D4D4D">

    </RelativeLayout>

</android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

DrawerLayout drawerLayout;
NavigationView navigation_view;
ImageView view;
int count = 1;

private boolean isOutSideClicked = false;


RelativeLayout relativeLayout;
private ActionButton actionButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    view = (ImageView)findViewById(R.id.view);

    if(getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }


    drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    drawerLayout.setScrimColor(Color.parseColor("#00000000"));
    navigation_view = (NavigationView) findViewById(R.id.navigation_view);
    relativeLayout = (RelativeLayout)findViewById(R.id.right_navigation);

    actionButton = (ActionButton) findViewById(R.id.fab_activity_action_button);

    actionButton.setImageResource(R.drawable.fab_plus_icon);
    actionButton.setRippleEffectEnabled(true);
    actionButton.setShadowRadius(0);
    actionButton.setShadowXOffset(0);
    actionButton.setShadowYOffset(0);
    actionButton.setButtonColor(Color.parseColor("#0072BA"));
    actionButton.setButtonColorPressed(Color.parseColor("#004F80"));
    actionButton.setShadowRadius(10);

    actionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(count == 1) {
                actionButton.moveLeft(200);
                actionButton.setImageResource(R.drawable.fab_plus_icon);
                drawerLayout.openDrawer(Gravity.RIGHT);
                view.setVisibility(View.VISIBLE);
                actionButton.bringToFront();
               count = count - 1;
            }else if(count == 0){

                closeFab();
            }
            else {

            }
        }
    });

}

private void closeFab(){
    view.setVisibility(View.GONE);
    actionButton.move(new MovingParams(MainActivity.this, 200 , 0));
    actionButton.setImageResource(R.drawable.fab_plus_icon);
    count = count + 1;
    drawerLayout.closeDrawer(Gravity.RIGHT);
}



  @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (drawerLayout.isDrawerOpen(navigation_view)) {

                View content = findViewById(R.id.drawer_layout);
                int[] contentLocation = new int[2];
                content.getLocationOnScreen(contentLocation);
                Rect rect = new Rect(contentLocation[0],
                        contentLocation[1],
                        contentLocation[0] + content.getWidth(),
                        contentLocation[1] + content.getHeight());


                if (!(rect.contains((int) event.getX(), (int) event.getY()))) {
                    isOutSideClicked = true;
                } else {
                    isOutSideClicked = false;
                    this.closeFab();
                }

            }
        }

        return super.dispatchTouchEvent(event);
    }


}

截图:

更新代码:

 if (!(rect.contains((int) event.getX(), (int) event.getY()))) {
                isOutSideClicked = true;
                 this.closeFab();

            } else {
                isOutSideClicked = false;


            }

I 当我在抽屉关闭的任何地方单击 NavigationView 时。但是当且仅当我单击按钮时,我想关闭抽屉。

更新:

流正常画面

点击按钮

点击外部区域

更改为此结果

dispatchTouchEvent() 覆盖中,您似乎想要检测 NavigationView 之外的点击,如果是,则触发 ActionButton 上的移动,因为发生这种情况时,抽屉将关闭。但是,您的代码实际上是在检测 DrawerLayout 内任何位置的点击,并在错误条件下触发移动。

最简单的修复方法是将 View content = findViewById(R.id.drawer_layout); 更改为 View content = findViewById(R.id.navigation_view);,并将 this.closeFab(); 行移动到 if-else 所在的 if 块。

View content = findViewById(R.id.navigation_view);
...

if (!(rect.contains((int) event.getX(), (int) event.getY()))) {
    isOutSideClicked = true;
    this.closeFab();
} else {
    isOutSideClicked = false;
}

您可以在抽屉上设置锁定模式

 mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);

当您想关闭它时,只需将其解锁即可。

https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html#setDrawerLockMode%28int,%20int%29