Home > Mobile >  How to make bottom navigation selected automatically when moving from one fragment to another?
How to make bottom navigation selected automatically when moving from one fragment to another?

Time:11-30

I'm trying to make action button to move from the home fragment to the promo fragment.

In the fragment home there is an action button that will move to the promo fragment. However, when the promo action button on the home fragment is clicked, the display will move to the promo fragment but the bottom navigation is still selected in the home fragment.

Screenshot Home Fragment

Screenshot Promo Fragment

This is my action button code in Home Fragment:

        imagePromo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((MainActivity)getActivity()).selectFragment(2);
        }
    });

This is my selectfragment code in MainActivity:

    public void selectFragment(int position){
    viewPager.setCurrentItem(position, true);
    viewPager.dispatchSetSelected(true);
}

This is my bottom navigation code in MainActivity:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    view = findViewById(android.R.id.content);
    viewPager = findViewById(R.id.viewpager);
    viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
    viewPager.setOffscreenPageLimit(pager_number);
    navigation = findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(item -> {
        switch (item.getItemId()) {
            case R.id.navigation_home:
                viewPager.setCurrentItem(0);
                return true;
            case R.id.navigation_category:
                viewPager.setCurrentItem(1);
                return true;
            case R.id.navigation_promo:
                viewPager.setCurrentItem(2);
                return true;
            case R.id.navigation_wishlist:
                viewPager.setCurrentItem(3);
                return true;
        }
        return false;
    });
}

CodePudding user response:

You can set it in your setOnClickListener to change the Fragment of the ViewPager and it's CurrentItem to get the icon changed aswell.

   Button to_promo = view.findViewById(R.id.to_promo);
        to_promo.setOnClickListener(view1 -> {
            viewPager.setCurrentItem(2);
        });

CodePudding user response:

I'm trying to use the code I found on github. Below is the code snippet:

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }
        @Override
        public void onPageSelected(int position) {
            if (prevMenuItem != null) {
                prevMenuItem.setChecked(false);
            } else {
                navigation.getMenu().getItem(0).setChecked(false);
            }
            navigation.getMenu().getItem(position).setChecked(true);
            prevMenuItem = navigation.getMenu().getItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

I tried to apply it in MainActivity and it solved my problem above.

  • Related