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 中单击其选项卡时加载片段数据
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- URL编码Swift iOS 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- UITextView 内容插图 2022-01-01