我更改了工具栏汉堡包图标但是当我点击

时间:2015-05-13 15:15:31

标签: android navigation-drawer android-toolbar slidingdrawer

我正在使用安卓工具栏而不是操作栏,您可以在下面的代码中看到。我用我的客户想要的东西改变了汉堡图标。但是当我点击图标时,抽屉菜单没有打开。这是我的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    toolbar=(Toolbar)findViewById(R.id.tool_bar);
    toolbar.inflateMenu(R.menu.menu_event_list);
    toolbar.setNavigationIcon(R.drawable.icon_menu);
    toolbar.setOnMenuItemClickListener(menuClicked);
    toolbar.setTitle(getResources().getString(R.string.app_name));

    mRecyclerView=(RecyclerView)findViewById(R.id.RecyclerView);
    mRecyclerView.setHasFixedSize(true);

    mAdapter=new MenuAdapter(titles,icons,u.fullName,u.email,R.drawable.a,this);
    mRecyclerView.setAdapter(mAdapter);

    mLayoutManager=new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    Drawer=(DrawerLayout)findViewById(R.id.DrawerLayout);
    mDrawerToggle=new ActionBarDrawerToggle(this,Drawer,toolbar,R.string.app_name,R.string.app_name){
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            // code here will execute once the drawer is opened( As I dont want anything happened whe drawer is
            // open I am not going to put anything here)
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            // Code here will execute once drawer is closed
        }
    };
    mDrawerToggle.setHomeAsUpIndicator(R.drawable.icon_menu);
    mDrawerToggle.setDrawerIndicatorEnabled(false);
    Drawer.setDrawerListener(mDrawerToggle);
    mDrawerToggle.syncState();
}

和xml for this

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar"
        />

</RelativeLayout>
<android.support.v7.widget.RecyclerView
    android:id="@+id/RecyclerView"
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:background="#ffffff"
    android:scrollbars="vertical">

</android.support.v7.widget.RecyclerView>

2 个答案:

答案 0 :(得分:2)

根据ActionBarDrawerToggle documentation

  应该从您的Activity syncState()方法调用

onPostCreate,以便在DrawerLayout的实例状态恢复后以及状态可能分歧时的任何其他时间进行同步。

在州结算后,请考虑将您的通话移至syncState()onPostCreate

答案 1 :(得分:0)

缺少活动中的一些覆盖

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (mDrawerToggle.onOptionsItemSelected(item)) {
      return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}