【问题标题】:How to disable BottomNavigationView while something is running in the selected Fragment?如何在所选片段中运行某些内容时禁用 BottomNavigationView?
【发布时间】:2020-04-23 06:10:36
【问题描述】:

我在当前项目中使用了 BottomNavigationView。我有四个片段。这是一个 SpeedTest 应用程序。如果我在运行速度测试时尝试切换到另一个 Fragment,应用程序会崩溃。我认为是在运行时禁用底部栏。但我不知道该怎么做。有没有办法做到这一点?

这是主要活动

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"/>

【问题讨论】:

  • 你能添加你的代码和崩溃报告吗?只需切换选项卡,它就不会崩溃。
  • 你能更具体地解释你的问题吗?您的“底部栏”是什么,是布局、底部导航视图、操作栏吗?究竟是什么导致了崩溃?您能否发布一些相关代码或最小的可重现示例:stackoverflow.com/help/minimal-reproducible-example
  • 其实就是BottomNavigationView,
  • 当我尝试切换片段时,它会导致 NullPointerException。但是如果我在测试完成后切换片段不会出现问题

标签: java android android-fragments bottomnavigationview bottombar


【解决方案1】:

您可以像下面这样禁用bottomNavigationBar:

bottomNavigationBar.setEnabled(false);

或者你也可以像下面这样隐藏bottomNavigationBar:

bottomNavigationBar.setVisibility(View.GONE);

【讨论】:

  • 但我必须在另一个片段中禁用它,而不是在主 Activity 中。
  • 你可以像这样获取 mainactivity 对象:((MainActivity)getActivity()).bottomNavigationBar
  • 如果您在不同的包中使用它们,请确保您的 bottomNavigationBar 对象是公开的
  • 它正在工作.. 这是我这边的一个错误。无论如何感谢您的帮助
  • 好吧,快乐编码:)
【解决方案2】:

只需要声明

 BottomNavigationView bottomNav; 

this 作为全局变量

然后放

 bottomNav = getActivity().findViewById(R.id.bottom_navigation); 

在 OncreateView() 中

【讨论】:

    【解决方案3】:

    您需要单独禁用每个菜单项:

        for (i in 0 until bottomNavigationView.menu.size()) {
            mainBinding.bottomNavigationView.menu.getItem(i).isEnabled = false
        }
    

    【讨论】:

      猜你喜欢
      • 2021-04-20
      • 2018-02-13
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多