how load fragment in ViewPager only when its selected(仅在选中时如何在 ViewPager 中加载片段)
问题描述
我在 Viewpager
中使用了 3 个 Fragments
,问题是我在 Asynctask
和加载器中加载大数据.在像 HTC one
这样的设备上,它工作得很好,但是,在低端设备上,它需要很多时间.这主要是因为当我实现 pagerAdapter
时,我将 Fragments 放在了一个 ArrayList
中,这会在加载主 Activity 时强制 Fragments 实例化.我需要的是它只是加载"第一个片段(主),当用户 Swype
时,加载另一个片段.它有什么方法可以实现这一目标?这是我的页面适配器
I'm using 3 Fragments
inside a Viewpager
, the problem is that I am loading big data in an Asynctask
and loaders. On devices like HTC one
, it works fine, however, on low-end devices, it takes a lot of time. This is mainly because when I implement the pagerAdapter
, I put the Fragments inside an ArrayList
, this force the fragments instantiate when the main activity is loaded. What I need is that it just "load" the first fragment (main) and when the user Swype
, load the other fragment. its any way to achieve this? this is my pageAdapater
public class PagerAdapter extends FragmentPagerAdapter {
private final ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
// private final ArrayList<String> titulos = new ArrayList<String>();
// private int NUM_PAGES =0;
public PagerAdapter(FragmentManager manager,int num_pages) {
super(manager);
// this.NUM_PAGES = num_pages;
}
public void addFragment(Fragment fragment,String title) {
mFragments.add(fragment);
notifyDataSetChanged();
}
@Override
public int getCount() {
//return NUM_PAGES;
return mFragments.size();
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
}
推荐答案
Sun 的上述方法对我不起作用(也许对你有用),但我想我也会分享我对他方法的编辑.顺便说一句Sun,很好的方法!
The above method from Sun did not work for me (maybe it does for you), but I thought I would share my edit of his method also. Very good method by the way Sun!
private boolean _hasLoadedOnce= false; // your boolean field
@Override
public void setUserVisibleHint(boolean isFragmentVisible_) {
super.setUserVisibleHint(true);
if (this.isVisible()) {
// we check that the fragment is becoming visible
if (isFragmentVisible_ && !_hasLoadedOnce) {
new NetCheck().execute();
_hasLoadedOnce = true;
}
}
}
这篇关于仅在选中时如何在 ViewPager 中加载片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:仅在选中时如何在 ViewPager 中加载片段
基础教程推荐
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01