Does FragmentPagerAdapter instantiateItem work with fragments?

I am trying to get my FragmentPagerAdapter to work, but the examples are a bit simplified for real life:

@Override public Object instantiateItem(final ViewGroup container, final int position) { final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); int resId = 0; switch (position) { case 0: resId = R.layout.favorites_fragment; break; case 1: resId = R.layout.nearby_list_fragment; break; default: throw new IllegalArgumentException("Index cant be mapped to fragement:" + position); } final View view = inflater.inflate(resId, null); viewPager.addView(view, 0); return view; } 

Since I want to use Fragments, I already confused my this method, since the whole example could find inflating the view instead of creating a fragment there. How to do it?

PS: Im using ActionBarSherlock lib 4.1 ...

Thanks Jens

+4
source share
1 answer

What exactly are you trying to do? Show the definition of your class. You must extend the FragmentPagerAdapter and implement public abstract Fragment getItem(int position) to return your fragments. Do not override instantiateItem() , it already does the right thing in the FragmentPagerAdapter .

+3
source

All Articles