The way I would do it is, I would first add three methods similar to this one (each for a single fragment. Replace the layout name and the fragment object to the appropriate fragment that is being switched to):
public void switchToFragment1() {
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.your_fragment_layout_name, new Fragment1()).commit();
}
So your switch statement would end up looking like this:
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
switchToFragment1();
break;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
switchToFragment2();
break;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
switchToFragment3();
break;
}
As for switching the fragments by swiping to the sides, I believe you would need a ViewPager.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…