簡體   English   中英

當且僅當我單擊按鈕時,我才想關閉導航抽屜

[英]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;


            }

當我在抽屜關閉的任何地方單擊NavigationView 但是,當且僅當我單擊“按鈕”時,我才想關閉抽屜。

更新:

流量正常畫面

在此處輸入圖片說明

點擊按鈕

在此處輸入圖片說明

點擊外面的區域

在此處輸入圖片說明

改變這個結果

在此處輸入圖片說明

dispatchTouchEvent()重寫中,您似乎想檢測到NavigationView之外的點擊,並觸發ActionButton上的點擊,因為這會導致抽屜關閉。 但是,您的代碼實際上是在DrawerLayout內的任何位置檢測點擊,並在錯誤的條件下觸發移動。

最簡單的解決方法是更改View content = findViewById(R.id.drawer_layout); View content = findViewById(R.id.navigation_view); ,然后移動this.closeFab(); if-elseif塊的行。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM