简体   繁体   中英

Sending list of fragments to FragmentPagerAdapter

im trying to make my FragmentPagerAdapter implementation reusable by sending list of fragments to pager adpater.

public Fragment getItem(int position) {
Fragment fragment = mFragmentList.get(position);
    if (fragment==null)
    {
      fragment=Fragment.instantiate(mContext,fragmentName????)
    }
}

Pager adapter dont uses fragment manager so tag is not defined there. Solution may be to specify fragments tag in xml but i would like to keep managing my fragments dynamically. Guess answer is in properly implementing inheritance... Any thoughts?

If you take a look at the documentation for Fragment.instantiate [here]( http://developer.android.com/reference/android/app/Fragment.html#instantiate(android.content.Context , java.lang.String, android.os.Bundle)), you'll see that the fragment name is not the same as the fragment's tag. It's the name of the class of your fragment. So if your fragment class is MyCustomFragment , you would create it using the following:

Fragment.instantiate(mContext, MyCustomFragment.class.getName());

But, generally best practice is to use a static newInstance method for fragment creation. You can read more on that here .

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