How to load fragment data only when its tab is clicked in PagerSlidingTabStrip(如何仅在 PagerSlidingTabStrip 中单击其选项卡时加载片段数据)
问题描述
我在我的项目中使用 PagerSlidingTabStrip
并以片段的形式显示数据.它是一个很棒的图书馆,效果很好.每个片段都包含从 Web 服务加载的学者讲座.访问 Web 服务的代码在 Fragment 的 onCreateView
方法中.目前所有片段的数据都是在添加到ViewPager
的同时加载的.我想知道是否有任何方式仅在用户单击/滑动到特定选项卡时才加载片段的数据.不是一次全部.
I am using PagerSlidingTabStrip
in my project and am showing data in fragments. Its a great library which works great. Each Fragment consists of a scholar's lectures which are loaded from a web service. The code to access the web service is in onCreateView
method of the Fragment. Currently all the fragment's data is loaded at the same time when they are added to ViewPager
. I was wondering if there is any way of loading the Fragment's data only when a user clicks/swipes to a specific tab. Not everything at a time.
当前代码非常标准,我从提供的示例 [android 解决方案] 中获得了帮助.1
Current code is pretty standard, I took help from the provided example [android solution].1
Pager Adapter 如下:
Pager Adapter is as follows:
public class ScholarDetailPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = { "Scholar Info", "Lectures"};
Scholar _scholar;
public ScholarDetailPagerAdapter(FragmentManager fm, Scholar scholar) {
super(fm);
_scholar = scholar;
}
@Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
@Override
public Fragment getItem(int arg0) {
if(arg0 == 0)
return ScholarsInfoFragment.netInstance(_scholar.Name, _scholar.Information, _scholar.ThumbnailUrl);
else
return ScholarLecturesFragment.newInstance(_scholar.ScholarId);
}
@Override
public int getCount() {
return TITLES.length;
}
}
它返回两个片段,Info 和 Lectures.Lectures Fragment 调用 Web 服务来获取学者的讲座.
It returns TWO fragments, Info and Lectures. Lectures fragment calls a web service to get a scholar's lectures.
活动代码为:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the Above View
setContentView(R.layout.tabbed_view);
Scholar scholar = getScholar(getIntent());
_tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
_pager = (ViewPager) findViewById(R.id.pager);
_adapter = new ScholarDetailPagerAdapter(getSupportFragmentManager(), scholar);
_pager.setAdapter(_adapter);
final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
.getDisplayMetrics());
_pager.setPageMargin(pageMargin);
_tabs.setViewPager(_pager);
}
我尝试添加 _tabs.setOnPageChangeListener(this);
并最终在我的活动中添加了 onPageSelected(int arg0)
.我以为现在很容易.然后我在上面的ScholarDetailPagerAdapter
中修改了public Fragment getItem(int arg0)
,返回null
而不是返回两个片段.但这引发了一个例外.我以为它会在寻呼机中呈现空视图..但我当然错了.
I tried adding _tabs.setOnPageChangeListener(this);
and ended up having onPageSelected(int arg0)
in my activity as well. I thought It would be easy now. Then I modified public Fragment getItem(int arg0)
in ScholarDetailPagerAdapter
above and returned null
instead of returning two fragments. But that raised an exception. I thought It would render empty views in pager .. but of course I was wrong.
为了确保 Fragment 在其各自的选项卡被单击且可见时加载数据,我还尝试覆盖其以下事件并在其中编写数据加载调用:
To make sure Fragment loads data when its respective tab is clicked and is visible, I also have tried overriding its following events and wrote the data loading call in them:
onResume()
没用.在活动中添加时仍然加载数据
onResume()
Not worked. Still loads data when added in activity
尝试覆盖 onHiddenChanged
Tried overriding onHiddenChanged
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if(!hidden){
loadLectures();
}
}
这也不起作用.没有加载数据,就好像根本没有触发此事件一样.
This did not work too. No data loaded as if this event is not fired at all.
我该怎么做?我只是想在用户单击选项卡时加载一个片段.
How do I do that? I simply want to load a fragment when user clicks on a tab.
提前致谢
推荐答案
您可以覆盖片段的 setUserVisibleHint
事件以了解其是否对用户可见,然后加载您的数据.类似于以下内容:
You could override setUserVisibleHint
event of the fragment to know if its visible to the user and then load your data. something like following:
boolean _areLecturesLoaded = false;
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser && !_areLecturesLoaded ) {
loadLectures();
_areLecturesLoaded = true;
}
}
这篇关于如何仅在 PagerSlidingTabStrip 中单击其选项卡时加载片段数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何仅在 PagerSlidingTabStrip 中单击其选项卡时加载片段数据
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01