简体   繁体   中英

navigation menu item not highlighted on switch of fragment1 to fragment2 with button click in fragment1

I have two fragments in my app namely HomeFragment and FeedbackFragment. In addition to switching fragments on tab buttons, I would also need to switch between fragments on a button click that is within the fragment. The tab items are highlighted properly on switching fragments using tab clicks. But the tab items does not get highlighted when i switch to other fragment on a button click from fragment1

Below is the code used to switch across fragments on button click and it works.

    FragmentTransaction fragmentTransaction =  getActivity().getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment, fragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

But the respective tab icon of FeedbackFragment is not highlighted. Currently the navigation item of HomeFragment remains highlighted even after FeedbackFragment is replaced. How do I highlight the menu item of Feedback Fragment ?

I tried below approaches but nothing worked:

BottomNavigationView bottomNavigationView = (BottomNavigationView)   findViewById(R.id.navigation);

View view = bottomNavigationView.findViewById(R.id.tab_calls);
view.performClick();

Also,

  MainActivity.mBottomBar.selectTabAtPosition(2);

Nothing worked. Please help.

尝试使用该方法选择一个标签,就像它被点击一样:

bottomNavigationView.setSelectedItemId(R.id.tab_calls)

What I have done now is in onResume of each fragment I try to de highlight all other tab bars and highlight the current tab bar again. I access the bottomNavigationMenu from each fragment and do this. Not sure if this is the right way to handle it but it works atleast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM