1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, StopFragment.OnButtonClickedListener {
private Toolbar toolbar;
private DrawerLayout drawerLayout;
private NavigationView navigationView;
private Fragment fragmentNews;
private Fragment fragmentProfile;
private Fragment fragmentMap;
private static final int FRAGMENT_NEWS=0;
private static final int FRAGMENT_PROFILE=1;
private static final int FRAGMENT_MAPS=2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.configureToolbar();
this.configureDrawerLayout();
this.configureNavigationView();
this.showFirstFragment();
}
@Override
public void onBackPressed()
{
if (this.drawerLayout.isDrawerOpen(GravityCompat.START))
{
this.drawerLayout.closeDrawer(GravityCompat.START);
}
else {
super.onBackPressed();
}
}
@Override
public boolean onNavigationItemSelected(MenuItem item)
{
int id = item.getItemId();
switch(id)
{
case R.id.activity_main_drawer_news:
this.showFragment(FRAGMENT_NEWS);
break;
case R.id.activity_main_drawer_profile:
this.showFragment(FRAGMENT_PROFILE);
break;
case R.id.activity_main_drawer_settings:
this.showFragment(FRAGMENT_MAPS);
break;
default:
break;
}
this.drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
private void showFirstFragment()
{
Fragment visibleFragment = getSupportFragmentManager().findFragmentById(R.id.main_frame_layout);
if (visibleFragment == null){
this.showFragment(FRAGMENT_NEWS);
this.navigationView.getMenu().getItem(0).setChecked(true);
}
}
@Override
public void onButtonClicked(View v)
{
Intent intent = new Intent(this, EventActivity.class);
startActivity(intent);
}
private void showFragment(int fragmentIdentifier)
{
switch(fragmentIdentifier)
{
case FRAGMENT_NEWS:
if (this.fragmentNews == null) this.fragmentNews = NewsFragment.newInstance();
if (!fragmentNews.isVisible()){
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_frame_layout, fragmentNews).commit();
}
break;
case FRAGMENT_PROFILE:
if (this.fragmentProfile == null) this.fragmentProfile = StopFragment.newInstance();
if (!fragmentProfile.isVisible()){
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_frame_layout, fragmentProfile).commit();
}
break;
case FRAGMENT_MAPS:
if (this.fragmentMap == null) this.fragmentMap = MapsFragment.newInstance();
if (!fragmentMap.isVisible()) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_frame_layout, fragmentMap).commit();
}
}
}
private void configureToolbar()
{
this.toolbar = (Toolbar)findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);
}
private void configureDrawerLayout()
{
this.drawerLayout = (DrawerLayout)findViewById(R.id.activity_main_drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout, toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
}
private void configureNavigationView()
{
this.navigationView = (NavigationView)findViewById(R.id.main_nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
} |
Partager