How to highlight the item when pressed using BottomNavigationView between activities?

前端 未结 4 1255
情深已故
情深已故 2020-12-10 02:45

I have added Bottom Navigation View for my app but I need the Bottom Navigation View between activities instead of fragment so I have added this code to Java for all my 3 ac

4条回答
  •  佛祖请我去吃肉
    2020-12-10 03:09

    You can use BottomNavigationView with activities representing tabs. The key is to repeat the navigation view component in each activity, and have the code of each activity control the navigation component: starting the right activity on navigation item clicks and selecting a proper navigation item after the activity has started.

    You need to start newly selected activities (tabs) with a delay as to allow the tab switching animation to complete before the new activity replaces the previous one.

    My approach was to have all activities representing tabs inherit from the same BaseActivity class implementing the common behavior.

    This is the code of an example BaseActivity:

    public abstract class BaseActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
    
        protected BottomNavigationView navigationView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(getContentViewId());
    
            navigationView = (BottomNavigationView) findViewById(R.id.navigation);
            navigationView.setOnNavigationItemSelectedListener(this);
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            updateNavigationBarState();
        }
    
        // Remove inter-activity transition to avoid screen tossing on tapping bottom navigation items
        @Override
        public void onPause() {
            super.onPause();
            overridePendingTransition(0, 0);
        }
    
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            navigationView.postDelayed(() -> {
                int itemId = item.getItemId();
                if (itemId == R.id.navigation_home) {
                    startActivity(new Intent(this, HomeActivity.class));
                } else if (itemId == R.id.navigation_dashboard) {
                        startActivity(new Intent(this, DashboardActivity.class));
                } else if (itemId == R.id.navigation_notifications) {
                        startActivity(new Intent(this, NotificationsActivity.class));
                }
                finish();
            }, 300);
            return true;
        }
    
        private void updateNavigationBarState(){
            int actionId = getNavigationMenuItemId();
            selectBottomNavigationBarItem(actionId);
        }
    
        void selectBottomNavigationBarItem(int itemId) {
            Menu menu = navigationView.getMenu();
            for (int i = 0, size = menu.size(); i < size; i++) {
                MenuItem item = menu.getItem(i);
                boolean shouldBeChecked = item.getItemId() == itemId;
                if (shouldBeChecked) {
                    item.setChecked(true);
                    break;
                }
            }
        }
    
        abstract int getContentViewId();
    
        abstract int getNavigationMenuItemId();
    
    }
    

    This is my whole example based on the Android Studio's Bottom Navigation Activity template:

    https://github.com/ddekanski/BottomNavigationViewBetweenActivities

提交回复
热议问题