为什么用BottomNavigation视图显示和隐藏片段不起作用?

时间:2018-06-23 13:28:38

标签: android android-fragments bottomnavigationview fragmentmanager

我正在使用BottomNavigationView及其中的5个片段。我希望每个片段仅创建一次,并且每次用户在它们之间切换时都不会重新创建。

因此,我正在创建所有片段,并尝试在片段容器中显示和隐藏它们。

这是我的代码: (我使用此help编写此代码)

public class MainActivity extends AppCompatActivity {

  private Fragment ideaFragment = IdeaFragment.newInstance();
  private Fragment articleFragment = ArticleFragment.newInstance();
  private Fragment videoFragment = VideoFragment.newInstance();
  private Fragment magFragment = MagFragment.newInstance();
  private Fragment mainListFragment = MainListFragment.newInstance();


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(view);


    btmNavigationView.setSelectedItemId(R.id.nav_home);
    btmNavigationView.setOnNavigationItemSelectedListener(navListener);

    fm = ((FragmentActivity) activity).getSupportFragmentManager();

    MainPresenter mainPresenter = new MainPresenter((MainListContract.View) mainListFragment, appRepository);
    ArticlePresenter articlePresenter = new ArticlePresenter((ArticleContract.View) articleFragment, appRepository);
    IdeaPresenter ideaPresenter = new IdeaPresenter((IdeaContract.View) ideaFragment, appRepository);
    VideoPresenter videoPresenter = new VideoPresenter((VideoContract.View) videoFragment, appRepository);
    MagPresenter magPresenter = new MagPresenter((MagContract.View) magFragment, appRepository);


    fm.beginTransaction().add(R.id.fragment_container, magFragment, "5").commit();
    fm.beginTransaction().add(R.id.fragment_container, ideaFragment, "4").commit();
    fm.beginTransaction().add(R.id.fragment_container, videoFragment, "3").commit();
    fm.beginTransaction().add(R.id.fragment_container, articleFragment, "2").commit();
    fm.beginTransaction().add(R.id.fragment_container, mainListFragment, "1").commit();

  }

  //setting up bottom navigation menu
  BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

      //Fragment currentFragment = ((FragmentActivity) activity).getSupportFragmentManager().findFragmentById(R.id.fragment_container);


      switch (menuItem.getItemId()) {

        case R.id.nav_home:

          if (active != mainListFragment)
            fm.beginTransaction().show(mainListFragment).commit();
          else
            fm.beginTransaction().hide(active).show(mainListFragment).commit();
          active = mainListFragment;


        case R.id.nav_article:

          if (active != articleFragment)
            fm.beginTransaction().show(articleFragment).commit();
          else
            fm.beginTransaction().hide(active).show(articleFragment).commit();
          active = articleFragment;


        case R.id.nav_video:

          if (active != videoFragment)
            fm.beginTransaction().show(videoFragment).commit();
          else
            fm.beginTransaction().hide(active).show(videoFragment).commit();
          active = videoFragment;


        case R.id.nav_idea:

          if (active != ideaFragment)
            fm.beginTransaction().show(ideaFragment).commit();
          else
            fm.beginTransaction().hide(active).show(ideaFragment).commit();
          active = ideaFragment;


        case R.id.nav_mag:

          if (active != magFragment)
            fm.beginTransaction().show(magFragment).commit();
          else
            fm.beginTransaction().hide(active).show(magFragment).commit();
          active = magFragment;

      }

      return true;
    }
  };
}

我的问题是,MainListFragment将在打开应用程序时显示,但是当我尝试转到其他片段时,什么也没发生! 并且MainListFragment不会改变!

我在哪里错了?

谢谢您的回答。

1 个答案:

答案 0 :(得分:1)

只留下带有replace的主要对象,而不是add

fm.beginTransaction().replace(R.id.fragment_container, magFragment, "5").commit();

并将其从onCreate

中删除
fm.beginTransaction().add(R.id.fragment_container, ideaFragment, "4").commit();
fm.beginTransaction().add(R.id.fragment_container, videoFragment, "3").commit();
fm.beginTransaction().add(R.id.fragment_container, articleFragment, "2").commit();
fm.beginTransaction().add(R.id.fragment_container, mainListFragment, "1").commit();

然后,在onNavigationItemSelected上执行以下操作:

switch (menuItem.getItemId()) {
    case R.id.nav_home:
        fm.beginTransaction().replace(R.id.fragment_container, magFragment, "5").commit();
        break;
    case R.id.nav_article:
        fm.beginTransaction().replace(R.id.fragment_container, articleFragment, "2").commit();
        break;
    case R.id.nav_video:
        fm.beginTransaction().replaceR.id.fragment_container, videoFragment, "3").commit();
        break;
    case R.id.nav_idea:
        fm.beginTransaction().replace(R.id.fragment_container, ideaFragment, "4").commit();
        break;
    case R.id.nav_mag:
        fm.beginTransaction().replace(R.id.fragment_container, mainListFragment, "1").commit();
        break;
}

实例化onCreate中的每个片段是正确的方法,这样,您就不会在每次切换时都创建另一个片段。如果要实现这种行为,则必须使用类似以下的方法:

fm.beginTransaction().replace(R.id.fragment_container, new MagPresenter((MagContract.View) magFragment, appRepository), "1").commit();