如何在所选片段中有 运行 时禁用 BottomNavigationView?
How to disable BottomNavigationView while something is running in the selected Fragment?
我在当前项目中使用了BottomNavigationView。我有四个片段。这是一个速度测试应用程序。如果我在速度测试为 运行 时尝试切换到另一个片段,应用程序会崩溃。我认为是在 运行 时禁用 Bottombar。但我不知道该怎么做。有什么办法吗?
这是主要的Activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
if (savedInstanceState == null){
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new SpeedFragment()).commit();
}
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener = new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()){
case R.id.nav_speed:
selectedFragment=new SpeedFragment();
break;
case R.id.nav_web:
selectedFragment=new WebFragment();
break;
case R.id.nav_video_test:
selectedFragment=new VideoFragment();
break;
case R.id.nav_history:
selectedFragment=new HistoryFragment();
break;
}
assert selectedFragment != null;
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();
return true;
}
};
}
这是activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_navigation"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_navigation"
android:background="?android:attr/windowBackground"
app:labelVisibilityMode="labeled"/>
您可以禁用 bottomNavigationBar,如下所示:
bottomNavigationBar.setEnabled(false);
或者您也可以隐藏 bottomNavigationBar,如下所示:
bottomNavigationBar.setVisibility(View.GONE);
刚要申报
BottomNavigationView bottomNav;
这是一个全局变量
并放
bottomNav = getActivity().findViewById(R.id.bottom_navigation);
在 OncreateView() 内部
您需要分别禁用每个菜单项:
for (i in 0 until bottomNavigationView.menu.size()) {
mainBinding.bottomNavigationView.menu.getItem(i).isEnabled = false
}
我在当前项目中使用了BottomNavigationView。我有四个片段。这是一个速度测试应用程序。如果我在速度测试为 运行 时尝试切换到另一个片段,应用程序会崩溃。我认为是在 运行 时禁用 Bottombar。但我不知道该怎么做。有什么办法吗?
这是主要的Activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
if (savedInstanceState == null){
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new SpeedFragment()).commit();
}
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener = new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()){
case R.id.nav_speed:
selectedFragment=new SpeedFragment();
break;
case R.id.nav_web:
selectedFragment=new WebFragment();
break;
case R.id.nav_video_test:
selectedFragment=new VideoFragment();
break;
case R.id.nav_history:
selectedFragment=new HistoryFragment();
break;
}
assert selectedFragment != null;
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,selectedFragment).commit();
return true;
}
};
}
这是activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_navigation"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="@menu/bottom_navigation"
android:background="?android:attr/windowBackground"
app:labelVisibilityMode="labeled"/>
您可以禁用 bottomNavigationBar,如下所示:
bottomNavigationBar.setEnabled(false);
或者您也可以隐藏 bottomNavigationBar,如下所示:
bottomNavigationBar.setVisibility(View.GONE);
刚要申报
BottomNavigationView bottomNav;
这是一个全局变量
并放
bottomNav = getActivity().findViewById(R.id.bottom_navigation);
在 OncreateView() 内部
您需要分别禁用每个菜单项:
for (i in 0 until bottomNavigationView.menu.size()) {
mainBinding.bottomNavigationView.menu.getItem(i).isEnabled = false
}