toolbar.setNavigationOnClickListener不起作用

toolbar.setNavigationOnClickListener这个函数不工作,不知道为什么。

activity_main.xml布局

      

container_main.xml布局

      

Java Activity类

 import android.app.Fragment; import android.app.FragmentManager; import android.content.res.TypedArray; import android.os.Bundle; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v4.widget.DrawerLayout; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.View; import android.widget.ListView; public class MainActivity extends AppCompatActivity { private DrawerLayout mDrawerLayout; private ListView mDrawerList; private ActionBarDrawerToggle mDrawerToggle; // slide menu items private String[] navMenuTitles; private TypedArray navMenuIcons; private ArrayList navDrawerItems; private NavDrawerListAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // enabling action bar app icon and behaving it as toggle button Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); toolbar.setNavigationIcon(R.drawable.ic_drawer); setSupportActionBar(toolbar); // load slide menu items navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); // nav drawer icons from resources navMenuIcons = getResources() .obtainTypedArray(R.array.nav_drawer_icons); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.list_slidermenu); navDrawerItems = new ArrayList(); // adding nav drawer items to array // Home navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1))); // Find People navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1))); // Photos navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1))); // Communities, Will add a counter here navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1))); // Recycle the typed array navMenuIcons.recycle(); // setting the nav drawer list adapter adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems); mDrawerList.setAdapter(adapter); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name, R.string.app_name ){ public void onDrawerClosed(View view) { //getActionBar().setTitle(mTitle); // calling onPrepareOptionsMenu() to show action bar icons invalidateOptionsMenu(); } public void onDrawerOpened(View drawerView) { //getActionBar().setTitle(mDrawerTitle); // calling onPrepareOptionsMenu() to hide action bar icons invalidateOptionsMenu(); } @Override public void onDrawerSlide(View drawerView, float slideOffset) { super.onDrawerSlide(drawerView, slideOffset); } }; mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("COMECOU", "COMECOU"); } }); mDrawerLayout.setDrawerListener(mDrawerToggle); if (savedInstanceState == null) { // on first time display view for first nav item displayView(0); } } /** * Displaying fragment view for selected nav drawer list item * */ private void displayView(int position) { // update the main content by replacing fragments Fragment fragment = null; switch (position) { case 0: fragment = new HomeFragment(); break; case 1: fragment = new FindPeopleFragment(); break; case 2: fragment = new PhotosFragment(); break; case 3: fragment = new CommunityFragment(); break; case 4: fragment = new PagesFragment(); break; case 5: fragment = new WhatsHotFragment(); break; default: break; } if (fragment != null) { FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.frame_container, fragment).commit(); // update selected item and title, then close the drawer mDrawerList.setItemChecked(position, true); mDrawerList.setSelection(position); setTitle(navMenuTitles[position]); mDrawerLayout.closeDrawer(mDrawerList); } else { // error in creating fragment Log.e("MainActivity", "Error in creating fragment"); } } } 

我忘了说如果我滑动我可以看到菜单只是按钮没有记录既没有被触发。

UPDATE

如果您想知道如何解决,请查看我的答案。

要记住的另一件事是

调用

toolbar.setNavigationOnClickListener()

之前

setSupportActionBar(toolbar);

不行。

我看到你正在使用ActionBarDrawerToggle ,因此setNavigationOnClickListener()是错误的。 右边是setToolbarNavigationClickListener() 。 这将使其行为像切换按钮。

 mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("TOU AQUI", "TOU AQUI"); } }); 

我发现错误,我们不能将工具栏放在DrawerLayout之外

这是它的样子:

             

在您的情况下,您应该使用ActionBarDrawerToggle#setToolbarNavigationClickListener

来自Android文档:

使用工具栏构造DrawerToggle时,它会在“导航”图标上设置单击侦听器。 如果要在禁用DrawerToggle时监听导航图标上的点击(setDrawerIndicatorEnabled(boolean)),则应该使用侦听器调用此方法,并且DrawerToggle会在禁用抽屉指示符时将单击事件转发到该侦听器。

在我的情况下 – ScrollView处理了所有触摸。 我在工具栏之后添加了android:layout_marginTop="?android:attr/actionBarSize" ,它已经有效了。 希望我能帮助别人)

      ... ...