简体   繁体   English

单击工具栏上的汉堡包图标不会打开导航抽屉

[英]Clicking hamburger icon on Toolbar does not open Navigation Drawer

I have this nav drawer which was working perfectly fine. 我有这个导航抽屉 ,工作正常。 Refactoring my code I removed all onOptionsItemSelecteds in activities and made all activities inherit from a base activity which extends AppComplatActivity and implements all the necessary methods. 重构代码,我删除了活动中的所有onOptionsItemSelecteds ,并使所有活动都继承自基本活动,该基本活动扩展了AppComplatActivity实现了所有必要的方法。 After this clicking on hamburger icon does not work any more even though I have syncstate () and every thing. 在此之后,即使我具有syncstate ()和所有事物,单击汉堡包图标也不再起作用。

Any clues why this is not working? 任何线索为什么这不起作用?

One of the activities: 活动之一:

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) {
}

} }

BaseActivity: BaseActivity:

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);
}

} }

You're using the four-parameter constructor for ActionBarDrawerToggle , which means you'll have to call the toggle's onOptionsItemSelected() method in MainActivity 's onOptionsItemSelected() override in order to open/close the drawer. 您正在为ActionBarDrawerToggle使用四参数构造函数,这意味着您必须在MainActivityonOptionsItemSelected()重写中调用切换的onOptionsItemSelected()方法才能打开/关闭抽屉。

For example: 例如:

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

    return super.onOptionsItemSelected(item);
}

If you happen to be providing your own Toolbar – eg, as the support ActionBar (though it's not necessary to set it as such) – then you can instead pass that Toolbar as the third argument in the ActionBarDrawerToggle constructor call. 如果碰巧提供了自己的Toolbar (例如,作为对ActionBar的支持(尽管不必如此设置)),则可以将该Toolbar作为ActionBarDrawerToggle构造函数调用中的第三个参数传递。 For example: 例如:

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

The drawer opening/closing will then be handled by ActionBarDrawerToggle internally, and you won't need to call through to the toggle in onOptionsItemSelected() . 然后,抽屉的打开/关闭将由ActionBarDrawerToggle内部进行处理,您无需在onOptionsItemSelected()调用切换按钮。

The setDisplayHomeAsUpEnabled() call is also unnecessary for this setup, which is handy if you don't want to set the Toolbar as the ActionBar . 此设置也不需要setDisplayHomeAsUpEnabled()调用,如果您不想将Toolbar设置为ActionBar ,这将非常方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击工具栏上的汉堡图标不会打开导航抽屉 - Clicking hamburger icon on Toolbar does not open Navigation Drawer 单击汉堡图标不会打开导航抽屉 - Clicking Hamburger Icon does not open Navigation Drawer 按下工具栏中的汉堡包图标时,导航抽屉无法打开 - Navigation drawer does not open when pressing the hamburger icon in toolbar 单击汉堡包图标有时无法在android中打开导航抽屉 - Clicking on hamburger icon sometimes does not open navigation drawer in android 点击后导航抽屉上的Android汉堡包图标无响应 - Android hamburger icon on navigation drawer does not respond after clicking 通过单击工具栏上的后退箭头,从子片段返回后,Hamburger Icon无法在父片段上打开导航抽屉 - Hamburger Icon not opening Navigation Drawer on parent fragment after returning from child fragment by clicking back arrow on toolbar 汉堡图标不会显示在导航抽屉片段中 - Hamburger Icon does not show in Navigation Drawer Fragment 在菜单图标上未打开导航抽屉,在工具栏上单击 - Navigation drawer not open on menu icon click on toolbar 导航抽屉主页图标不会恢复为汉堡图标 - Navigation drawer home icon does not restore to hamburger icon android - 点击应用图标打开导航抽屉? - android - open navigation drawer by clicking the app icon?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM