2017-01-20 2 views
0

작업 표시 줄에 서랍 레이아웃이있는 Android 앱을 개발 중입니다. 서랍을 열거 나 닫을 때들을 수 있도록 drawerlayout의 리스너를 추가했습니다. 드로어 레이아웃에 첨부 된 탐색 뷰가 있습니다.Android 햄버거 메뉴 상호 작용

서랍은 열림, 열림, 열림, 닫힘 및 열림 - 닫힘과 같은 다양한 방법으로 열고 닫을 수 있습니다. 이벤트를 추가하여 각 이벤트를 개별적으로 식별해야합니다. 나는이 방법으로 drawerlayout 및 navigationview을 추가 한

: 활동에

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

<LinearLayout 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    > 
    <include 
     android:id="@+id/toolbar" 
     layout="@layout/tool_bar" 
    /> 
    <FrameLayout 
     android:id="@+id/frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    </FrameLayout> 

</LinearLayout> 

<android.support.design.widget.NavigationView 
    android:id="@+id/navigation_view" 
    android:layout_height="match_parent" 
    android:layout_width="wrap_content" 
    android:layout_gravity="start" 
    app:headerLayout="@layout/header" 
    app:menu="@menu/drawer" 
    /> 

노, 나는 navigationview 및 drawerlayout에서 다음 청취자를 추가했습니다.

//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu 
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 

     // This method will trigger on item Click of navigation menu 
     @Override 
     public boolean onNavigationItemSelected(MenuItem menuItem) { 


      //Checking if the item is in checked state or not, if not make it in checked state 
      if(menuItem.isChecked()) menuItem.setChecked(false); 
      else menuItem.setChecked(true); 

      //Closing drawer on item click 
      drawerLayout.closeDrawers(); 

      //Check to see which item was being clicked and perform appropriate action 
      switch (menuItem.getItemId()){ 

       // Switch case identify different options 

      } 
     } 
    }); 

    // Initializing Drawer Layout and ActionBarToggle 
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer); 
    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){ 

     @Override 
     public void onDrawerClosed(View drawerView) { 
      // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank 
      super.onDrawerClosed(drawerView); 
     } 

     @Override 
     public void onDrawerOpened(View drawerView) { 
      // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank 

      super.onDrawerOpened(drawerView); 
     } 
    }; 

    //Setting the actionbarToggle to drawer layout 
    drawerLayout.setDrawerListener(actionBarDrawerToggle); 

    //calling sync state is necessay or else your hamburger icon wont show up 
    actionBarDrawerToggle.syncState(); 

이제 onDrawerOpenedonDrawerClosed은 서랍을 열고 닫을 때에 대해 나에게 정보를 제공합니다. 상호 작용을 열거 나 닫을 때 어떻게 구별합니까? 식별 할 상호 작용 : tap-to-open, swipe-to-open, swipe-to-close and tap-outside-to-close.

답변

0

onDrawerClosed, onDrawerOpened 이상의 방법을 제공합니다. 당신은 슬라이드를 검출하기 위해 onDrawerSlide 메소드를 오버라이드 (override) : 도구 모음에서

.. 
@Override 
public void onDrawerSlide (View drawerView,float slideOffset){ 

    if(determine if openning or closing){ 
    doStuff(); 
    }else{ 
    doOtherStuff(); 
    } 
} 

드린다는 Activity.onOptionsItemSelected(MenuItem item) 방법을 재정 의하여 deteced 할 수 있습니다. 그런 다음 해당 항목의 ID가 android.R.id.home인지 확인하십시오.

더 여기 읽기 :

 @Override 
     public void onDrawerStateChanged(int newState) { 

      if (newState == DrawerLayout.STATE_DRAGGING) { 

       if (drawerLayout.isDrawerOpen(GravityCompat.START)) { 
        // drawer is currently open and is being closed by sliding 
       } else { 
        // drawer is currently closed and is being opened by sliding 
       } 
      } 
     } 
내가 검지 추가합니다

: https://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html

0

나는 정확한 방법을 발견했다 결정 서랍은 다른 방법을, 슬라이딩 일부 기존 매개 변수를 사용하여 작성하여 연 경우 일단 알게되면 탭 이벤트가 올바르게 나타납니다. 누군가 아는 사람은 의견을 말하십시오.