BottomNavigationView - 如何取消选中所有MenuItem并保持标题显示?

Vic*_*ira 12 android title menuitem bottomnavigationview

因为我喜欢这个设计,BottomNavigationView所以我决定用它来实现我的应用程序的新菜单,而不仅仅是使用简单的按钮.

我把这篇文章作为指导.

BottomNavigationView文件,其目的是为了

提供应用程序顶级视图之间的快速导航.它主要设计用于移动设备.

在我的情况下,我只希望每个人MenuItem都启动一个活动,但默认情况下总是MenuItem选择一个:

在此输入图像描述

我尝试将颜色设置为白色:

app:itemIconTint="@color/white"
app:itemTextColor="@color/white"
Run Code Online (Sandbox Code Playgroud)

仍然,明显选择MenuItem与其他(标题大小更大)不同,这仍然困扰我:

在此输入图像描述

我想出了一个隐藏MenuItem的选择,如:

<item
android:id="@+id/uncheckedItem"
android:title="" />
Run Code Online (Sandbox Code Playgroud)

并提出自己的观点GONE:

 bottomNavigationView.getMenu().findItem(R.id.uncheckedItem).setChecked(true);
 bottomNavigationView.findViewById(R.id.uncheckedItem).setVisibility(View.GONE);
Run Code Online (Sandbox Code Playgroud)

这使得所有MenuItem BottomNavigationView都未被选中,但默认情况下是隐藏标题,因为它有超过3个MenuItems要显示,即使第四个MenuItem要解决GONE:

在此输入图像描述

所以我的问题仍然存在,是否有/取消选择所有MenuItem并保持其标题显示?

pao*_*olo 24

要取消选择我创建此扩展程序的所有项目:

fun BottomNavigationView.uncheckAllItems() {
    menu.setGroupCheckable(0, true, false)
    for (i in 0 until menu.size()) {
        menu.getItem(i).isChecked = false
    }
    menu.setGroupCheckable(0, true, true)
}
Run Code Online (Sandbox Code Playgroud)

menu.setGroupCheckable(0, true, false) 使其成为可能。第三个参数使菜单不是排他的,然后在循环中更改选中状态。完成再次将菜单设置为独占。

这里的文档


Ron*_*ler 18

mNavigationBottom.getMenu().setGroupCheckable(0, false, true);
Run Code Online (Sandbox Code Playgroud)

  • 这会取消选择所有项目而没有任何副作用.但是,您必须稍后放入"mNavigationBottom.getMenu().setGroupCheckable(0,true,true);" 在某处允许项目再次显示为选中状态. (5认同)

itt*_*nyu 5

谢谢你的想法.我在我的lib中实现它.通过反思,我有更好的方法.所以它不会显示空间.

如果您有兴趣.点击这里:https://github.com/ittianyu/BottomNavigationViewEx

private void initBottomViewAndLoadFragments(final BottomNavigationViewEx bnve) {
    bnve.enableAnimation(false);
    bnve.enableShiftingMode(false);
    bnve.enableItemShiftingMode(false);

    // use the unchecked color for first item
    bnve.setIconTintList(0, getResources().getColorStateList(R.color.bnv_unchecked_black));
    bnve.setTextTintList(0, getResources().getColorStateList(R.color.bnv_unchecked_black));

    bnve.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {

        private boolean firstClick = true;
        private int lastItemId = -1;

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            // restore the color when click
            if (firstClick) {
                firstClick = false;
                bnve.setIconTintList(0, getResources().getColorStateList(R.color.selector_bnv));
                bnve.setTextTintList(0, getResources().getColorStateList(R.color.selector_bnv));
            }

            if (firstClick || lastItemId == -1 || lastItemId != item.getItemId()) {
                lastItemId = item.getItemId();
            } else {
                return false;
            }

            // do stuff
            return fillContent(item.getItemId());
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

- res/color/selector_bnv.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/bnv_checked_white" android:state_checked="true" />
    <item android:color="@color/bnv_unchecked_black" />
</selector>
Run Code Online (Sandbox Code Playgroud)

- res/values/colors.xml

<color name="bnv_checked_white">@android:color/white</color>
<color name="bnv_unchecked_black">@android:color/black</color>
Run Code Online (Sandbox Code Playgroud)