0

klicke Ich habe Navigationsschublade mit dem folgenden Code erstellt.Ich möchte die Navigationsschublade schließen, wenn und nur wenn ich auf die Schaltfläche

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


} 

ScreenShot:

enter image description here

Update-Code:

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

      } else { 
       isOutSideClicked = false; 


      } 

ich, wenn ich auf NavigationView auf eine beliebige Stelle ist die Schublade schließen. Aber ich möchte die Schublade schließen wenn und nur wenn ich auf Button klicke.

Update:

Flow normalen Bildschirm

enter image description here

Klick auf den Button

enter image description here

Klick auf Außenbereich

enter image description here

Änderung dieses Ergebnis

enter image description here

Antwort

1

Im dispatchTouchEvent() überschreiben, es sieht aus wie Sie Klicks ermitteln möchten, die außerhalb des NavigationView sind, und lösen die Bewegung auf Ihrem ActionButton, wenn sie , da die Schublade schließt, wenn das passiert. Ihr Code erkennt jedoch Klicks irgendwo innerhalb der DrawerLayout und löst die Bewegung in der falschen Bedingung aus.

Die einfachste Lösung ist View content = findViewById(R.id.drawer_layout);-View content = findViewById(R.id.navigation_view);, zu ändern und die this.closeFab(); Linie zum if Block der if-else es ist in Bewegung.

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

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