Created
January 15, 2020 18:22
-
-
Save NinoDLC/68b0a6cc0bfecc8161ab1219c9894daf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main_activity); | |
mBottomNavigationBar = findViewById(R.id.main_bottom_navigation_menu); | |
setupBottomNavigationBar(); | |
// Listen | |
mBottomNavigationBar.setOnTabSelectedListener((position, wasSelected) -> { | |
displayFragment(position); | |
return true; | |
}); | |
// Style matters | |
mBottomNavigationBar.setColored(true); | |
// Init | |
if (savedInstanceState == null) { | |
displayFragment(0); | |
} | |
} | |
private void displayFragment(int position) { | |
if (position == mLastBottomNavPosition) { | |
return; | |
} | |
Fragment fragToShow = null; | |
switch (position) { | |
case 0: // Appointments | |
fragToShow = AppointmentFragment.newInstance(); | |
break; | |
case 1: // News | |
fragToShow = NewsFragment.newInstance(); | |
break; | |
case 2: // Profile | |
fragToShow = ProfileFragment.newInstance(); | |
break; | |
} | |
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); | |
if (mLastBottomNavPosition != -1) { | |
if (mLastBottomNavPosition < position) { | |
transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left); | |
} else { | |
transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right); | |
} | |
} | |
transaction.replace(R.id.main_fragment_container, fragToShow).commit(); | |
mLastBottomNavPosition = position; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment