Я выполнил действие «Навигационный ящик», и вкладка «Активность» открывается при выборе элемента из ящика.Когда я изменяю вкладки в этом действии с вкладками (используя 8 фрагментов, которые я уже создал и объявил), он просто выходит из TabbedActivity и возвращается на начальную страницу действия в Навигаторе навигации.Как только я проведу пальцем, я не смогу увидеть следующий фрагмент, поскольку он просто возвращается.
Java-код MainActivity5:
public class Main5Activity extends AppCompatActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main5, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position) {
case 0:
Legends1 t1 = new Legends1();
return t1;
case 1:
Legends2 t2 = new Legends2();
return t2;
case 3:
Legends3 t3 = new Legends3();
return t3;
case 4:
Legends4 t4 = new Legends4();
return t4;
case 5:
Legends5 t5 = new Legends5();
return t5;
case 6:
Legends6 t6 = new Legends6();
return t6;
case 7:
Legends7 t7 = new Legends7();
return t7;
case 8:
Legends8 t8 = new Legends8();
return t8;
}
return null;
}
@Override
public int getCount(){
// Show 8 total pages.
return 8;
}
}
Пример одного из 8 фрагментов Java-файлов (всето же самое):
public class Legends1 extends Fragment {
public Legends1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_legends1, container, false);
}