0

I am trying to set up sliding tabs on top of an activity. I want this result :

enter image description here

I'm following the explanations of this example : http://developer.android.com/samples/SlidingTabsBasic/project.html

And I'm also looking at this video : https://www.youtube.com/watch?v=tRg_eDfQ8fk

I'm doing so because of this post : Action bar navigation modes are deprecated in Android L

I have 3 fragments for each of my tab. Each inflates a different layout and do different things. Now I need to connect them to my PageAdapter. I've already used an adaptor for a ListView. I used getView with the position to do my work

However with this PageAdapter I'm not sure what I need to proceed. Should I use this method to create my fragment :

 public Object instantiateItem(ViewGroup container, int position) {

and if so, how should it be done ?

Thank you.

Community
  • 1
  • 1
user2230304
  • 578
  • 1
  • 5
  • 14

1 Answers1

2

You must implement a FragmentPagerAdapter like this:

public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        return new FirstFragment();
    case 1:
        return new TwoFragment();
    case 2:
        return new ThreeFragment();
    }

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 3;
}


}

and set it to viewpager adapter:

 mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
 viewPager.setAdapter(mAdapter);
temp
  • 56
  • 2