简体   繁体   中英

FragmentPagerAdapter instantiateItem working with Fragments?

i try to get my FragmentPagerAdapter working, but the examples are a bit to easy 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;
}

As i'm want to use Fragments im already confused my this method as all example i could find inflate a view instead of create fragment there. How to do this ?

PS: Im using the ActionBarSherlock lib 4.1...

Thanks, Jens

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM