toolbar.setNavigationOnClickListener不起作用

时间:2015-10-24 16:00:38

标签: android toolbar drawerlayout

toolbar.setNavigationOnClickListener这个功能不起作用,不知道为什么。

activity_main.xml布局

<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".login.LoginActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />

container_main.xml布局

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Listview to display slider menu -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/list_background"
        android:choiceMode="singleChoice"
        android:divider="@color/list_divider"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

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

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

}

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

更新

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

7 个答案:

答案 0 :(得分:52)

要记住的另一件事是

<强>调用

toolbar.setNavigationOnClickListener()

之前

setSupportActionBar(toolbar);

赢了。

答案 1 :(得分:6)

我发现您使用的是ActionBarDrawerToggle,因此setNavigationOnClickListener()是错误的。正确的是setToolbarNavigationClickListener()。这将使其表现得像一个切换按钮。

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

答案 2 :(得分:1)

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

这是它的样子:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <!-- Framelayout to display Fragments -->
            <FrameLayout
                android:id="@+id/frame_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#2196F3"
                android:minHeight="?attr/actionBarSize"/>
        </FrameLayout>

        <!-- Listview to display slider menu -->
        <ListView
            android:id="@+id/list_slidermenu"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/list_background"
            android:choiceMode="singleChoice"
            android:divider="@color/list_divider"
            android:dividerHeight="1dp"
            android:listSelector="@drawable/list_selector" />

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

</FrameLayout>

答案 3 :(得分:0)

您应该在案例中使用ActionBarDrawerToggle#setToolbarNavigationClickListener

来自Android Documentation:

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

答案 4 :(得分:0)

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/rootView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/common_toolbar" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="?android:attr/actionBarSize"
        >

       <RelativeLayout
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
        >
    ... ...

答案 5 :(得分:0)

根本问题出在ActionBarDrawerToggle中。那就是构造函数的样子:

ActionBarDrawerToggle(Activity activity, Toolbar toolbar, DrawerLayout drawerLayout, DrawerArrowDrawable slider, @StringRes int openDrawerContentDescRes, @StringRes int closeDrawerContentDescRes) {
        //...
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDrawerIndicatorEnabled) {
                    toggle();
                } else if (mToolbarNavigationClickListener != null) {
                    mToolbarNavigationClickListener.onClick(v);
                }
            }
        });
}

我不能真正理解逻辑:

  1. toolbar.setNavigationOnClickListener设置汉堡包图标的点击侦听器
  2. 当用户点击汉堡包时:

    2.1如果看到汉堡包,则打开/隐藏抽屉

    2.2如果看不到汉堡,则呼叫点击监听器。

因此,在大多数情况下,汉堡包图标应该可见,因此在所有这些情况下,setNavigationOnClickListener无效且无用。

可能看起来应该像这样:

ActionBarDrawerToggle(Activity activity, Toolbar toolbar, DrawerLayout drawerLayout, DrawerArrowDrawable slider, @StringRes int openDrawerContentDescRes, @StringRes int closeDrawerContentDescRes) {

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                toggle();
                if (mToolbarNavigationClickListener != null) {
                    mToolbarNavigationClickListener.onClick(v);
                }
            }
        });
}

一种解决方法可能是覆盖toolbar.setNavigationOnClickListener并在那里调用toggle。尽管该方法是私有程序包:

package android.support.v7.app

import android.support.v4.widget.DrawerLayout
import android.support.v7.widget.Toolbar
import android.view.View


object SupportUtils {
    fun setHamburgerListener(listener: () -> Unit,
                             drawerToggle: ActionBarDrawerToggle,
                             drawerLayout: DrawerLayout,
                             toolbar: Toolbar) {
        drawerLayout.addDrawerListener(object : DrawerLayout.DrawerListener {
            override fun onDrawerStateChanged(newState: Int) {
                drawerToggle.onDrawerStateChanged(newState)
            }

            override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
                drawerToggle.onDrawerSlide(drawerView, slideOffset)
            }

            override fun onDrawerClosed(drawerView: View) {
                drawerToggle.onDrawerClosed(drawerView)
            }

            override fun onDrawerOpened(drawerView: View) {
                drawerToggle.onDrawerOpened(drawerView)
            }

        })

        toolbar.setNavigationOnClickListener {
            listener()
            drawerToggle.toggle()
        }
    }
}

答案 6 :(得分:0)

    private fun setUpToolbarWithHomeAsUp() {

        setSupportActionBar(toolbar)
        val actionBar = supportActionBar


        actionBar?.let{
            actionBar.setHomeButtonEnabled(true)
            actionBar.setDisplayShowTitleEnabled(false)
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                //What ever you want
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }