Вы можете попробовать это:
(в MainActivity.java)
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position) {
case 0:
Frag1 frag1 = new Frag1();//this is one activity
return frag1;
case 1:
Frag2 frag2 = new Frag2();
return frag2;
default:
return null;
}
}
@Override
public int getCount() {
return 2; //'the number 2 is 'cause you've got a two activities
}
}
и если вы хотите открыть другое действие с помощью кнопки с плавающим действием, вы можете попробовать это:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mViewPager.getCurrentItem()==0){
Fragment1();
Toast.makeText(MainActivity.this, "Example 1", Toast.LENGTH_SHORT).show();
}else if(mViewPager.getCurrentItem()==1){
Fragmen2();
Toast.makeText(MainActivity.this, "Example 2", Toast.LENGTH_SHORT).show();
}
}
});
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}
public void Fragment1(){//Call to the activity Example1
Intent fragment0 = new Intent(getApplicationContext(), example1.class);
startActivity(fragment0);
onResume();
}
public void Fragmen2(){ //Call to the activity Example2
Intent fragment1 = new Intent(getApplicationContext(), example2.class);
startActivity(fragment1);
onResume();
}