This question already has answers here:
ActionBarDrawerToggle with AppCompatActivity and Toolbar Back button with Fragments
                                
                                    (2个答案)
                                
                        
                        
                            Toolbar back button is not calling onOptionsItemSelected Android
                                
                                    (2个答案)
                                
                        
                                去年关闭。
            
                    
这是我的问题。抽屉单击时未调用onOptionsItemSelected()。这是我的代码,非常简单...

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;
private DrawerLayout drawer;
private ActionBarDrawerToggle toggle;
private RelativeLayout aboutUs, feedBack;
private View view;

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

    view = findViewById(R.id.mainView);
    aboutUs = (RelativeLayout) findViewById(R.id.list4);
    feedBack = (RelativeLayout) findViewById(R.id.list5);

    initToolbar();
    drawerInit();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        if (drawer.isDrawerOpen(Gravity.LEFT)) {
            drawer.closeDrawer(Gravity.LEFT);
        } else {
            drawer.openDrawer(Gravity.LEFT);
        }
    }
    return super.onOptionsItemSelected(item);
}

public void initToolbar() {
    toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
}

public void drawerInit() {
    drawer = (DrawerLayout) findViewById(R.id.drawer);

    toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close) {

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            float moveFactor = (drawerView.getWidth() * slideOffset);
            view.setTranslationX(moveFactor);
        }

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

    drawer.addDrawerListener(toggle);
    toggle.setDrawerIndicatorEnabled(true);
    toggle.syncState();
}

@Override
protected void onDestroy() {
    super.onDestroy();
}


}

我在optionsItemSelected回调中设置了断点,但是在抽屉单击时不起作用。

但是抽屉打开了。那么问题出在哪里,我该如何解决?感谢您的回答...........................

最佳答案

此activity.xml代码..

<?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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main2"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<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"
    app:headerLayout="@layout/nav_header_main2"
    app:menu="@menu/activity_main2_drawer" />

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

10-07 19:37
查看更多