0

I have class BottomNaviationViewHelper which I use for Bottom Naviation View. And I need to load fragments in this activity. But when i run my app nothing happens.

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();
        }
    });

 }

And this is one of my sample fragments 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);
}

I hope someone will help me to solve this problem.Thanks!

Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33

2 Answers2

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;
        }
-1

to change fragment use following code:

 public void loadFragment(Fragment targetFragment) {
    getSupportFragmentManager()
            .beginTransaction()
            .replace(frameLayout.getId(), targetFragment)
            .commit();
}
Riddhi Shah
  • 3,092
  • 5
  • 36
  • 56