【问题标题】:How to load multiple fragments in activity android如何在活动android中加载多个片段
【发布时间】:2018-07-18 23:09:47
【问题描述】:

我有用于底部导航视图的类 BottomNaviationViewHelper。 我需要在这个活动中加载片段。但是当我运行我的应用程序时,什么也没有发生。

BottomNaviationViewHelper.class

 public  void  setupBottomNavigationView(BottomNavigationViewEx bottomNavigationViewEx){
    Log.d(TAG, "setupBottomNavigationView: Setting up NavigationView1");
    bottomNavigationViewEx.enableAnimation(false);
    bottomNavigationViewEx.enableItemShiftingMode(false);
    bottomNavigationViewEx.enableShiftingMode(false);
    bottomNavigationViewEx.setTextVisibility(false);
}

 public  void enableNavigation(Context context, final BottomNavigationViewEx view, final FragmentManager supportFragmentManager){
    view.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment fragment;
            switch (item.getItemId()){
                case R.id.nav_home:
                    fragment = new FragmentMarker();
                    loadFragment(fragment);
                    return true;
                case R.id.nav_bookmark:
                    fragment = new FragmentBookmark();
                    loadFragment(fragment);
                    return true;
                case R.id.nav_blog:
                    fragment = new FragmentBlog();
                    loadFragment(fragment);
                    return true;
                case R.id.nav_notification:
                    fragment = new FragmentNotification();
                    loadFragment(fragment);
                    return true;
                case R.id.nav_account:
                    fragment = new FragmentAccount();
                    loadFragment(fragment);
                    return true;

            }
           return false;
        }

        private void loadFragment(Fragment fragment) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction transaction = supportFragmentManager.beginTransaction();
            transaction.replace(R.id.container, fragment);
            transaction.addToBackStack(null);
            transaction.commit();
        }
    });

 }

这是我的示例片段之一FragmentsBlog.class

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_blog, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setupBottomNavigationView();
}

private void setupBottomNavigationView(){
    Log.d(TAG, "setupBottomNavigationView: Setting up BottomNavigationView");
    BottomNavigationViewEx bottomNavigationViewEx = getView().findViewById(R.id.bottom_navigation);
    ((BottomNavigationViewHelper)getActivity()).setupBottomNavigationView(bottomNavigationViewEx);
    ((BottomNavigationViewHelper)getActivity()).enableNavigation(getActivity(), bottomNavigationViewEx, getFragmentManager());
    Menu menu = bottomNavigationViewEx.getMenu();
    MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
    menuItem.setChecked(true);
}

希望有人能帮我解决这个问题。谢谢!

【问题讨论】:

    标签: android android-fragments android-activity bottomnavigationview


    【解决方案1】:
    You also need to implement BottomNavigationView.OnNavigationItemSelectedListener
    
    
      @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment fragment = null;
    
                switch (item.getItemId()) {
                    case R.id.nav_home:
                    fragment = new FragmentMarker();
                    break;
    
                case R.id.nav_bookmark:
                    fragment = new FragmentBookmark();
                    break;
    
                case R.id.nav_blog:
                    fragment = new FragmentBlog();
                    break;
    
                case R.id.nav_notification:
                    fragment = new FragmentNotification();
                    break;
    
                case R.id.nav_account:
                    fragment = new FragmentAccount();
                    break;
                }
    
                return loadFragment(fragment);
            }
    
            private boolean loadFragment(Fragment fragment) {
                //switching fragment
                if (fragment != null) {
                    getSupportFragmentManager()
                            .beginTransaction()
                            .replace(R.id.fragment_container, fragment)
                            .commit();
                    return true;
                }
                return false;
            }
    

    【讨论】:

      【解决方案2】:

      使用以下代码更改片段:

       public void loadFragment(Fragment targetFragment) {
          getSupportFragmentManager()
                  .beginTransaction()
                  .replace(frameLayout.getId(), targetFragment)
                  .commit();
      }
      

      【讨论】:

      • 这正是问题中的代码正在做的事情。如果链接或不链接,这些方法的行为相同
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 2019-07-08
      相关资源
      最近更新 更多