如何禁用BottomNavigationView的点击和触摸?

14

我希望在特定条件下实现行为,当底部视图无法点击时,我想使底部视图项目被点击时不跳转到该项,而是仍停留在当前项目。

这是我的底部视图布局


在这里添加你的代码 - Abdul Waheed
我只想实现,但不知道如何做。 - Muhammad Aiman Bin Kamal
6个回答

26

如果您想禁用底部导航视图,您可以禁用菜单项。

private void enableBottomBar(boolean enable){
    for (int i = 0; i < mBottomMenu.getMenu().size(); i++) {
        mBottomMenu.getMenu().getItem(i).setEnabled(enable);
    }
}

mBottomMenu.menu.iterator().forEach { it.isEnabled = enable } mBottomMenu.menu.iterator().forEach { it.isEnabled = enable } - Amaury Ricardo
如何隐藏特定的选项卡,例如在横向模式下需要隐藏主页选项卡。 - sejn

12

Kotlin的一行代码风格:

bottom_navigation.menu.forEach { it.isEnabled = false }

Kotlin 一行代码 FTW! - C.Schone

2
<android.support.design.widget.BottomNavigationView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="false"
    android:contextClickable="false"/>

尝试使用以下代码。它可以禁用点击。

使用Java或Kotlin动态地,您可以禁用点击。

    bottomView.setEnabled(false); 
    bottomView.setFocusable(false); 
    bottomView.setFocusableInTouchMode(false);
    bottomView.setClickable(false); 
    bottomView.setContextClickable(false);
    bottomView.setOnClickListener(null);

将 onClick Listener 设置为 Null 可以禁用点击事件。
bottomView.menu.forEach { it.isEnabled = false }

5
我可以继续点击并选择这个项目。 - Muhammad Aiman Bin Kamal
bottomView.setOnClickListener(null); 尝试这个。 - Tomin B Azhakathu

1

您可以设置其子视图的触摸监听器。以下是使用android-ktx的示例:

bottomNav.children.forEach {
   (it as? ViewGroup)?.children?.forEach {
       it.setOnTouchListener { _, _ -> true } // or null to enable touch again
   }
}

0
你可以这样做:
bottomNavigation.menu.iterator().forEach { it.isEnabled = !error }

0
public class CustomBottomNavigationView extends BottomNavigationView {

    ...

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        ViewGroup menuView = (ViewGroup) getChildAt(0);
        if (menuView != null) {
            for (int i = 0; i < menuView.getChildCount(); i++) {
                menuView.getChildAt(i).setEnabled(enabled);
            }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接