Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

android - When is FragmentPagerAdapter's getItem called?

I'm writing an application that uses the FragmentPagerAdapter. The fragments in the adapter need to be updated according to outside data - but that doesn't happen. I noticed that the fragment classes are only instantiated once, in the getItem function:

@Override
public Fragment getItem(int position) {
    TabInfo info = mTabs.get(position);
    return Fragment.instantiate(mContext, info.clss.getName(),
                info.args);
}

Even if I delete the class and use a new one, nothing helps - this method is only called once, the first time that the tab is populated, and then never again. Anyone has an idea why?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

getItem will be called whenever the adapter needs a fragment and the fragment does not exist.

If the fragment already exists in the FragmentManager then there is no need to instantiate it and getItem does not need to be called.

To update an existing fragment you would need to retrieve it from the FragmentManager or the adapter and manipulate it accordingly.

By default, the viewpager will create fragments for the visible page and the one next to it. I.e to start with, fragments in position 1 and 2. When you swipe to page 2, the fragment at position 3 will be created etc


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...