问题描述
我使用的是ViewPager插入到FragmentStatePagerAdapter。每个片段包含一个ListView。在此列表视图我希望显示广告做出一个分析服务器调用。我只想当用户导航到一个片段(所以我不能使用onCreateView或片段onActivityCreated事件),使这些电话。有没有我可以勾上为此事件?
I am using a ViewPager plugged to a FragmentStatePagerAdapter. Each fragment contains a listview. In this listview I wish to display ads and make calls to a analytics server. I only want to make those calls when the user navigates to a fragment (so I cannot use the onCreateView or onActivityCreated events in the Fragment). Is there an event I can hook on to this end ?
更新
我意识到,我获取我的当前片段的方法是flawe
UpdateI realized that the way I am fetching my current fragment is flawe
@Override
public void onPageSelected(int page) {
this.currentPage = page;
tabsAdapter.getItem(page);
}
的getItem(INT位置)
在寻呼机实际上是负责instanciating的片段,所以这将创建一个新的独立的片段(因此 getActivity( )
返回null)。我想我会为我们的做法,我保持地图的片段(<位置,片断>
)的,我会从地图上删除片段时destoryItem被称为在pagerAdapter。
getItem(int position)
in the pager is actually responsible for instanciating the fragments, so it would create a new unattached fragment (hence getActivity()
returning null). I think I will us an approach where I keep a map of the fragments (<Position, Fragment>
), i will remove the fragment from the map when destoryItem is called in the pagerAdapter.
推荐答案
您是对的,最好的选择是这里的第二个解决方案:
的
You are right, best option is the second solution here:http://tamsler.blogspot.nl/2011/11/android-viewpager-and-fragments-part-ii.html
让你的想法是正确的。
这篇关于ViewPager + FragmentStatePagerAdapter:需要知道的是显示片段时(选择)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!