【问题标题】:Clicking hamburger icon on Toolbar does not open Navigation Drawer单击工具栏上的汉堡包图标不会打开导航抽屉
【发布时间】:2016-08-02 21:35:03
【问题描述】:

我有这个 导航抽屉,它工作得非常好。 重构我的代码,我删除了活动中的所有onOptionsItemSelecteds,并使所有活动都继承自扩展AppComplatActivity实现所有必要方法的基本活动。 在此之后,即使我有 syncstate() 和所有东西,点击汉堡图标也不再起作用。

任何线索为什么这不起作用?

其中一项活动:

public class MainActivity extends BaseActivity implements SearchFilterFragment.OnFragmentInteractionListener {

NavigationView navigationView;
DrawerLayout drawerLayout;

private Tracker mTracker;

@Override
protected void onResume() {
    super.onResume();
    drawerLayout.openDrawer(GravityCompat.START);
}

@Override
protected void onPostResume() {
    super.onPostResume();
    mTracker.setScreenName("MainActivity" + "-----");
    mTracker.send(new HitBuilders.ScreenViewBuilder().build());
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerLayout.openDrawer(GravityCompat.START);
    navigationView = (NavigationView) findViewById(R.id.navigation_view_primary);
    navigationView.setNavigationItemSelectedListener(new NavigationDrawerListener(this));
    setupToolbar();
    Haftdong application = (Haftdong) getApplication();
    mTracker = application.getDefaultTracker();
}

private void setupToolbar() {
    // Show menu icon
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);// will make the icon clickable and add the < at the left of the icon.
    DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerToggle.syncState();//for hamburger icon
}

@Override
public void onFragmentInteraction(Uri uri) {
}

}

基础活动:

public class BaseActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_base, menu);
    return true;
}

@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.
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

【问题讨论】:

    标签: android navigation-drawer android-optionsmenu hamburger-menu


    【解决方案1】:

    您正在为ActionBarDrawerToggle 使用四参数构造函数,这意味着您必须在MainActivityonOptionsItemSelected() 覆盖中调用切换的onOptionsItemSelected() 方法才能打开/关闭抽屉。

    例如:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    

    如果你碰巧提供了自己的Toolbar——例如,作为支持ActionBar(尽管没有必要这样设置)——那么你可以将Toolbar作为第三个参数传递给ActionBarDrawerToggle 构造函数调用。例如:

    Toolbar toolbar = findViewById(R.id.toolbar);
    ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
            toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    

    然后抽屉的打开/关闭将由ActionBarDrawerToggle 在内部处理,您无需调用onOptionsItemSelected() 中的切换开关。

    此设置也不需要调用setDisplayHomeAsUpEnabled(),如果您不想将Toolbar 设置为ActionBar,这很方便。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多