[英]How can i set Listener to switch in Navigation drawer?
活动_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>
活动_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 代码中的开关
如何设置监听器切换
在准备导航视图菜单项的地方试试这个。
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
switchcompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//your action
}
});
由于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
}
});
我希望这会很有用。
菜单布局
<?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()
}
您不能使用 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
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.