Android导航抽屉图标不起作用

时间:2017-05-31 18:32:33

标签: java android navigation-drawer

我有一个工作的导航抽屉,但我更改了工具栏,现在导航工具的图标停止工作。拉出抽屉后再次工作。这是相关的代码:

MainActivity.java

//Drawer and fragments variables
private FragmentManager fragmentManager;
private ActionBarDrawerToggle toggle;
private DrawerLayout drawer;

//Searchbar variables
private Toolbar toolbar, searchtollbar;
private Menu search_menu;
private MenuItem item_search;

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

    //Setup toolbar
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    setSearchtollbar();

    //Setup navigationdrawer
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    navigationView.getMenu().getItem(0).setChecked(true);

    //Setup fragments
    fragmentManager = getSupportFragmentManager();
    try{
        fragmentManager.beginTransaction().replace(R.id.container, HomeFragment.class.newInstance()).commit();
    }catch(Exception e){
        handleError(e);
    }
}

@Override
public void onBackPressed() {
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
protected void onPostCreate(Bundle savedInstanceState){
    super.onPostCreate(savedInstanceState);
    toggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    toggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    //noinspection SimplifiableIfStatement
    Log.i("TAG", "Item clicked: "+item.getItemId());
    if (toggle.onOptionsItemSelected(item)) {
        return true;
    }
    switch(item.getItemId()){
        case R.id.action_settings:{
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            return true;
        }
    }
    return super.onOptionsItemSelected(item);
}

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    Log.i("TAG", "Navigation item clicked: " + id);
    Class fragmentClass;
    Fragment fragment = null;

    switch(id) {
        ...
    }

    try {
        fragment = (Fragment) fragmentClass.newInstance();
    } catch (Exception e) {
        handleError(e);
    }

    fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

main_activity.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:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <include
            android:id="@+id/toolbar"
            layout="@layout/app_bar_main"/>
        <include layout="@layout/app_bar_main_search"
            android:visibility="invisible"/>
        <include
            layout="@layout/main_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/toolbar"/>
    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:visibility="gone"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

现在我的问题是:

  • 为什么汉堡包图标第一次不起作用?
  • 为什么在拉出导航抽屉后才开始工作?

提前致谢。

2 个答案:

答案 0 :(得分:0)

设置抽屉监听器后添加切换同步并检查

drawer.addDrawerListener(toggle);
toggle.syncState();

答案 1 :(得分:0)

使用此代码:

 ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

toggle.setDrawerIndicatorEnabled(false); //disable "hamburger to arrow" drawable
        toggle.setHomeAsUpIndicator(R.drawable.menu); //set your own

        // setting click listener on hamburger icon
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawerLayout.openDrawer(Gravity.START);
                }
            }
        });

        // setting listerner on drawer
        mDrawerLayout.addDrawerListener(toggle);
        toggle.syncState();