如果您想禁用底部导航视图,您可以禁用菜单项。
private void enableBottomBar(boolean enable){
for (int i = 0; i < mBottomMenu.getMenu().size(); i++) {
mBottomMenu.getMenu().getItem(i).setEnabled(enable);
}
}
Kotlin的一行代码风格:
bottom_navigation.menu.forEach { it.isEnabled = false }
<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);
bottomView.menu.forEach { it.isEnabled = false }
您可以设置其子视图的触摸监听器。以下是使用android-ktx的示例:
bottomNav.children.forEach {
(it as? ViewGroup)?.children?.forEach {
it.setOnTouchListener { _, _ -> true } // or null to enable touch again
}
}
bottomNavigation.menu.iterator().forEach { it.isEnabled = !error }
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);
}
}
}
}