[英]Switch in Navigation drawer item with Design Support Library on Android
I need to put Switch inside item in navigation drawer.我需要将 Switch 放在导航抽屉中的项目内。 I'm using new design support library, but I cannot find if it is posibble at all.我正在使用新的设计支持库,但我根本找不到它是否可行。 When using使用时
android:checkable
item is just full selected and that is not what I wish.项目只是完全选择,这不是我想要的。
This is screenshot of what I really want.这是我真正想要的截图。 Is that possible to achieve that?有可能实现吗?
Your menu item for the navigation drawer:您的导航抽屉菜单项:
<item
android:id="@+id/nav_item1"
android:icon="@drawable/ic_item1"
android:title="item1"
app:actionLayout="@layout/layout_switch"
/>
and the layout for that item:以及该项目的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
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:text=""/>
</LinearLayout>
EDIT:编辑:
I ended up using a different approach.我最终使用了不同的方法。 In fact, I found out that you can use any view in the drawer, so there's no point in bothering with the menu stuff.事实上,我发现你可以使用抽屉中的任何视图,所以没有必要为菜单内容烦恼。 Just create a view the usual way (with listeners, etc.) and add in to the drawer.只需以通常的方式(使用侦听器等)创建一个视图并将其添加到抽屉中。
One way I have found of doing this would be to use setActionView on the menuItem you want:我发现这样做的一种方法是在您想要的 menuItem 上使用 setActionView :
mNavigationView.getMenu().findItem(R.id.nav_connect)
.setActionView(new Switch(this));
// To set whether switch is on/off use:
((Switch) mNavigationView.getMenu().findItem(R.id.nav_connect).getActionView()).setChecked(true);
Probably want a click listener as well to change the state of the Switch:可能还需要单击侦听器来更改 Switch 的状态:
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_connect:
((Switch) menuItem.getActionView()).toggle();
return true;
}
}
}
I haven't tried, but you could probably use android:actionLayout="@layout/switch_layout" in xml and point to a custom layout you created.我还没有尝试过,但是您可能可以在 xml 中使用 android:actionLayout="@layout/switch_layout" 并指向您创建的自定义布局。
Also could try using an ActionProvider which might offer a little more robustness.也可以尝试使用 ActionProvider,它可能会提供更多的健壮性。 I haven't tried this method either though.不过这个方法我也没试过。
None of the answers seems to be complete so after some more research here is what I came up with:似乎没有一个答案是完整的,所以经过更多的研究后,我得出了以下结论:
drawer_switch.xml: drawer_switch.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SwitchCompat
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_switch"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
drawer_menu.xml: drawer_menu.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">
<item android:title="@string/header">
<menu>
<item
android:id="@+id/switch_item"
android:icon="@drawable/ic_switch_item"
app:actionLayout="@layout/drawer_switch"
android:title="@string/switch_item" />
</menu>
</item>
</menu>
DrawerActivity.java: DrawerActivity.java:
SwitchCompat drawerSwitch = (SwitchCompat) navigationView.getMenu().findItem(R.id.switch_item).getActionView();
drawerSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// do stuff
} else {
// do other stuff
}
}
});
DrawerActivity.java: DrawerActivity.java:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.switch_item) {
return false;
}
closeDrawer();
return true;
}
I did something like this.我做了这样的事情。
navigationView.getMenu().findItem(R.id.vibrate)
.setActionView(new Switch(this));
Switch vibrateSwitch =(Switch)
navigationView.getMenu().findItem(R.id.vibrate).getActionView();
vibrateSwitch.setChecked(true);
vibrateSwitch.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked){
SharedPreferences sharedPreferences =
getSharedPreferences(getString(R.string.MyPREFERENCES), MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(getString(R.string.VIBRATE), isChecked);
editor.commit();
}
});
For those of you using Kotlin Extensions
对于那些使用Kotlin Extensions
<menu 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">
<item
android:id="@+id/nav_notifications_switch"
android:icon="@drawable/ic_notifications"
android:title="@string/notifications"
app:actionLayout="@layout/drawer_notifications_switch" />
</menu>
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toggleSwitch"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
nav_view.menu.findItem(R.id.nav_notifications_switch)
.actionView
.toggleSwitch
.setOnCheckedChangeListener { _, isChecked ->
}
simply yes you can do this easily, i will post my code so you can implement it是的,您可以轻松做到这一点,我将发布我的代码,以便您可以实现它
<item android:id="@+id/nav_item_1"
android:title="Android"
android:icon="@drawable/ic_android_black_24dp"
android:checked="true"/>
and the Main Activity Layout should be :主要活动布局应该是:
<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"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<TextView
android:id="@+id/content_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:text="Your Content Goes Here"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/colorAccent" />
<!-- your content layout -->
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/menu_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/menu_drawer" />
And here's a step by step tutorial for Navigation Drawer using Design Library这是使用设计库的导航抽屉的分步教程
You should be able to.你应该能够。
Your navigation drawer view can be a LinearLayout
, and inside that, you can include your NavigationView
and another ViewGroup
adding the switches.你的导航抽屉视图可以是一个LinearLayout
,在里面,你可以包含你的NavigationView
和另一个添加开关的ViewGroup
。
If you have to update the switch in navigation with respect to it's toggling then it's better to use two different menu layouts with checked switch and without checked switch.如果您必须在导航中更新切换开关,那么最好使用两种不同的菜单布局,选中开关和不选中开关。 So by checking the current state you can easily change the navigation drawer.因此,通过检查当前状态,您可以轻松更改导航抽屉。
In res
-> menu
-> menu.xml
create <item>
with field: app:actionViewClass
在res
-> menu
-> menu.xml
create <item>
with field: app:actionViewClass
example:例子:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:showIn="navigation_view">
<item android:title="">
<menu>
<group android:checkableBehavior="single">
<item
android:checkable="false"
android:id="@+id/is_night_mode"
android:icon="@drawable/main_navigation_night_mode_light"
android:title="@string/main_navigation_night_mode"
app:actionViewClass="com.google.android.material.switchmaterial.SwitchMaterial" />
</group>
</menu>
</item>
</menu>
Next in Activity
or Fragment
find actionView
like this:接下来在Activity
或Fragment
中找到actionView
是这样的:
val nightModeSwitch = binding.navView.menu.findItem(R.id.is_night_mode).actionView as SwitchMaterial
Set OnCheckedChangeListener
设置OnCheckedChangeListener
nightModeSwitch.setOnCheckedChangeListener { buttonView, isChecked ->
//do something
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.