Created
December 27, 2017 04:42
-
-
Save Velmm/4a0d8e6bd7cfca3efadcd2d5c7d60082 to your computer and use it in GitHub Desktop.
This file contains 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
public class MainActivity extends AppCompatActivity { | |
private ActionBar toolbar; | |
private BottomNavigationView bottomNavigationView; | |
private String[] menus ={"Latest","Favorite","Cart","Profile"}; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
toolbar = getSupportActionBar(); | |
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigationview); | |
bottomNavigationView.setOnNavigationItemSelectedListener(navigationItemSelectedListener); | |
toolbar.setTitle(menus[0]); | |
loadFragment(new LatestFragment()); | |
} | |
private BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() { | |
@Override | |
public boolean onNavigationItemSelected(@NonNull MenuItem item) { | |
Fragment fragment; | |
switch (item.getItemId()){ | |
case R.id.item1: | |
fragment = new LatestFragment(); | |
toolbar.setTitle(menus[0]); | |
loadFragment(fragment); | |
return true; | |
case R.id.item2: | |
toolbar.setTitle(menus[1]); | |
fragment = new FavoriteFragment(); | |
loadFragment(fragment); | |
return true; | |
case R.id.item3: | |
toolbar.setTitle(menus[2]); | |
fragment = new CartFragment(); | |
loadFragment(fragment); | |
return true; | |
case R.id.item4: | |
toolbar.setTitle(menus[3]); | |
fragment = new AccountFragment(); | |
loadFragment(fragment); | |
return true; | |
} | |
return false; | |
}; | |
}; | |
private void loadFragment(Fragment fragment){ | |
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); | |
fragmentTransaction.replace(R.id.frame_layout,fragment); | |
fragmentTransaction.commit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment