FragmentPagerAdapter的getItem何时调用? [英] When is FragmentPagerAdapter's getItem called?

查看:184
本文介绍了FragmentPagerAdapter的getItem何时调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用FragmentPagerAdapter的应用程序.适配器中的片段需要根据外部数据进行更新-但这不会发生.我注意到片段类在getItem函数中仅实例化一次:

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?

谢谢!

推荐答案

只要适配器需要一个片段并且该片段不存在,就会调用getItem.

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

如果FragmentManager中已经存在该片段,则无需实例化它,也不需要调用getItem.

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

要更新现有片段,您需要从FragmentManager或适配器中检索它,并进行相应的操作.

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

默认情况下,viewpager将为可见页面及其旁边的页面创建片段.即从位置1和2处的片段开始.当您滑动到第2页时,将在位置3处创建片段,等等.

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

这篇关于FragmentPagerAdapter的getItem何时调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆