2016-08-21 180 views
2

我正在弄亂材質導航抽屜,但不能滑出。當我按左上方的抽屜圖標時,導航抽屜不會彈出。我無法弄清楚我錯過了什麼。Android導航抽屜不能滑出

當我在屏幕上從左向右滑動時,它正確地滑出。但是通過點擊左上角的抽屜圖標不會顯示出來。

這是我的活動的一部分:

public class MainActivity extends AppCompatActivity { 
    private DrawerLayout drawerLayout; 
    private Toolbar toolbar; 
    private SectionsPagerAdapter mSectionsPagerAdapter; 
    private ViewPager mViewPager; 

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

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 
     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the activity. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.container); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

     initNavigationDrawer(); 
    } 

    public void initNavigationDrawer() { 

     NavigationView navigationView = (NavigationView)findViewById(R.id.navigation_view); 
     navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
      @Override 
      public boolean onNavigationItemSelected(MenuItem menuItem) { 

       int id = menuItem.getItemId(); 

       switch (id){ 
        case R.id.home: 
         Toast.makeText(getApplicationContext(),"Home", Toast.LENGTH_SHORT).show(); 
         drawerLayout.closeDrawers(); 
         break; 
        case R.id.settings: 
         Toast.makeText(getApplicationContext(),"Settings",Toast.LENGTH_SHORT).show(); 
         break; 
        case R.id.logout: 
         finish(); 
       } 
       return true; 
      } 
     }); 

     View header = navigationView.getHeaderView(0); 
     TextView tv_email = (TextView)header.findViewById(R.id.tv_email); 
     tv_email.setText("[email protected]"); 
     drawerLayout = (DrawerLayout)findViewById(R.id.drawer); 

     ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close){ 

      @Override 
      public void onDrawerClosed(View v){ 
       super.onDrawerClosed(v); 
      } 

      @Override 
      public void onDrawerOpened(View v) { 
       super.onDrawerOpened(v); 
      } 
     }; 
     drawerLayout.addDrawerListener(actionBarDrawerToggle); 
     actionBarDrawerToggle.syncState(); 
    } 
} 

這裏是activity_main:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="garagemonitor.studiosunmedia.garage.MainActivity"> 
     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/AppTheme.AppBarOverlay"> 

     <include 
      android:id="@+id/toolbar" 
      layout="@layout/tool_bar"> 
     </include> 

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

    <android.support.v4.view.ViewPager 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end|bottom" 
     android:layout_margin="@dimen/fab_margin" 
     android:visibility="invisible" 
     android:src="@android:drawable/ic_dialog_email" /> 
</android.support.design.widget.CoordinatorLayout> 
    <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/nav_header" 
     app:menu="@menu/menu_navigation"/> 
</android.support.v4.widget.DrawerLayout> 

Toolbar.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="?attr/colorPrimary" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark" 
    android:elevation="4dp"> 

</android.support.v7.widget.Toolbar> 
+0

是否滑出當你從左至右滑動? – Vucko

+0

是的,當我從屏幕左側左右滑動..我怎麼能滑動,當我點擊左上角的抽屜按鈕 –

回答

0

嘗試手動添加邏輯:

toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     drawerLayout.openDrawer(GravityCompat.START); 
    } 
}); 

這應該工作。

+1

,而不是'navigationView'我用'drawerLayout',它的工作 –

+0

啊,好的很酷。我沒有注意變量名:)考慮upvoting,如果它幫助你,這對我意味着。謝謝。 – Vucko

0

在代碼中,上述setSupportActionBar(欄)中,使用

工具欄=(欄)findViewById(R.id.toolbar);代替


工具條工具欄=(欄)findViewById(R.id.toolbar);

您重新初始化工具欄變量有,是什麼原因導致在onCreate方法正在使用其他工具欄可變initNavigationDrawer