如何设置侦听器以在导航抽屉中切换?

时间:2016-05-21 04:04:16

标签: android xml

actvity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav1"
        android:icon="@drawable/ic_lock_outline_black_24dp"
        android:title="test1" />
    <item
        android:id="@+id/nav2"
        android:icon="@drawable/ic_add_a_photo_black_24dp"
        android:title="test2" />
    <item
        android:id="@+id/nav3"
        android:icon="@drawable/ic_face_black_24dp"
        android:title="test3" />

</group>

activity_main.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/content_main" />

<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_main"
    app:menu="@menu/activity_main_drawer" />

onoff_toggle.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:id="@+id/switch_layout"
android:layout_height="match_parent">

<android.support.v7.widget.SwitchCompat
    android:id="@+id/drawer_switch"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:checked="false"
    android:textOff="off"
    android:textOn="on"
    app:showText="true"/>

</LinearLayout>

这是我在导航抽屉菜单项xml代码中的切换

如何设置Listener切换

4 个答案:

答案 0 :(得分:7)

在准备导航视图菜单项的位置尝试此操作。

NavigationView navigationView=(NavigationView)findViewById(id);

//get the menu from the navigation view
Menu menu=navigationView.getMenu();

//get switch view
SwitchCompat switchcompat=(SwitchCompat) MenuItemCompat.getActionView(menu.findItem(R.id.nav_notify)).findViewById(R.id.drawer_switch);

//add listener
switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    //your action
                }
            });

答案 1 :(得分:0)

您无法使用MenuItemCompat.getActionView(),因为它已弃用

 Menu menu_nav=navigationView.getMenu()
 MenuItem item=menu_nav.findItem(R.id.nav_geolocation);
 switcher=(SwitchCompat)item.getActionView().findViewById(R.id.drawer_switch);

  //add listener
  switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //your action
            }
        });

答案 2 :(得分:0)

由于不推荐MenuItemCompat.getActionView,所以我使用的代码如下:

MenuItem menuItem = navigationView.getMenu().findItem(R.id.app_bar_switch); // This is the menu item that contains your switch
drawer_switch = (Switch) menuItem.getActionView().findViewById(R.id.drawer_switch);
drawer_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // Your logic goes here
        }
});

我希望这可能有用。

答案 3 :(得分:0)

菜单布局

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:icon="@drawable/ic_notif"
        android:title="Notification"
        app:actionViewClass="android.widget.Switch"
        >
</menu>

代码

val mNavigationView = findViewById(R.id.nav_view)
val navMenu = mNavigationView.menu
val menuItem = navMenu.findItem(R.id.switch)
val switch = menuItem.getActionView() as Switch

switch.setOnCheckedChangeListener { buttonView, isChecked ->
    Toast.makeText(this, "Hello World, isChecked = $isChecked", Toast.LENGTH_LONG).show()
}