I want to have different tabs, where you can swipe through like in the android market. Each tab should use one fragment and use one method for it.
This is my Fragmen
Create the adapter:
class MyPagerAdapter extends FragmentPagerAdapter {
public List<String> fragmentsA;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
fragmentsA = fragments;
}
@Override
public Fragment getItem(int position) {
//return MyFragment.newInstance();
return Fragment.instantiate(context, fragmentsA.get(position));
}
@Override
public CharSequence getPageTitle(int position) {
//return CONTENT[position % CONTENT.length].toUpperCase();
return mEntries.get(position % CONTENT.length).toUpperCase();
}
@Override
public int getCount() {
// return CONTENT.length;
return mEntries.size();
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
Each fragment belongs to different classes.
So you have to define a fragment list (These go to the main FragmentActivity) :
static MyPagerAdapter adapter;
and fill it with the fragments:
fragments.add(ConnectionFragment.class.getName());
fragments.add(DataFragment.class.getName());
fragments.add(Tab3.class.getName());
//..etc
Then you have to add the fragments to the adapter like this:
pager = (ViewPager)findViewById(R.id.viewpager);
adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
The MyPagerAdaper will go through all the fragments contained by the fragments
list, then it will instantiate each of them.
In your FragmentPagerAdapter#getItem method, do not create object of the Fragments. You need to return the Fragment in the following way -
return Fragment.instantiate(context, ConnectionFragment.class.getName());
This means that the method will look like this -
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment =null;
switch (position) {
case 0:
fragment = Fragment.instantiate(context, ConnectionFragment.class.getName());
break;
case 1:
fragment = Fragment.instantiate(context, DataFragment.class.getName());
break;
case 2:
fragment = Fragment.instantiate(context, GraphFragment.class.getName());
break; }
return fragment;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
See this example:
public static class MyPagerAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 3;
public MyPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Returns total number of pages
@Override
public int getCount() {
return NUM_ITEMS;
}
// Returns the fragment to display for that page
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show FirstFragment
return FirstFragment.newInstance(0, "Page # 1");
case 1: // Fragment # 0 - This will show FirstFragment different title
return FirstFragment.newInstance(1, "Page # 2");
case 2: // Fragment # 1 - This will show SecondFragment
return SecondFragment.newInstance(2, "Page # 3");
default:
return null;
}
}
// Returns the page title for the top indicator
@Override
public CharSequence getPageTitle(int position) {
return "Page " + position;
}
}
With the release of ViewPager2 1.0.0, the FragmentPagerAdapter
and FragmentStatePagerAdapter
classes for interacting with ViewPager
have been deprecated.
ViewPager2
is an improved version of the ViewPager
library that offers enhanced functionality and addresses common difficulties with using ViewPager
.
ViewPager2
ViewPager2
ViewPager
, read this page to learn
more about migrating to ViewPager2
.