Problem with BottomNavigationView

0

When a item is selected from the menu it should be highlighted to make sure it is selected.

That works until I implement the OnNavigationItemSelectedListener

When I do this the bar continues to work but the highlighting does not work anymore, the same element is always highlighted.

My class

 public class MainActivity extends AppCompatActivity {

    BottomNavigationView bottomBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bottomBar = findViewById(R.id.navigation);
        bottomBar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch(item.getItemId()){
                    case R.id.action_home:
                        getSupportFragmentManager().beginTransaction().replace(R.id.frameContainer, new HomeFragment()).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
                        break;
                    case R.id.action_profile:
                        getSupportFragmentManager().beginTransaction().replace(R.id.frameContainer, new ProfileFragment()).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
                        break;
                    case R.id.action_search:
                        getSupportFragmentManager().beginTransaction().replace(R.id.frameContainer, new SearchFragment()).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
                        break;
                }
                return false;
            }
        });
    }


}

XML

<android.support.design.widget.BottomNavigationView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:layout_gravity="bottom"
    app:menu="@menu/my_navigation_items"
    />
    
asked by Mike 12.12.2017 в 19:28
source

1 answer

0

I already solved it

The solution was to change the return from false to true

bottomBar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch(item.getItemId()){
                case R.id.action_home:
                    getSupportFragmentManager().beginTransaction().replace(R.id.frameContainer, new HomeFragment()).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
                    break;
                case R.id.action_profile:
                    getSupportFragmentManager().beginTransaction().replace(R.id.frameContainer, new ProfileFragment()).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
                    break;
                case R.id.action_search:
                    getSupportFragmentManager().beginTransaction().replace(R.id.frameContainer, new SearchFragment()).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();
                    break;
            }
            return true;
        }
    });
    
answered by 18.12.2017 / 21:53
source