【发布时间】:2018-07-21 09:51:50
【问题描述】:
我有带有 7 个标签的 Viewpager,就像在 Play 商店中一样,如果我们点击主屏幕上的标签它会加载,如果我们再次点击该标签它不会加载,我希望如果我点击标签 1 它应该加载然后我让点击在标签 7 上,然后我再次点击标签 1,它不应该加载,只有在满足某些条件时才加载,我该怎么做?现在它只是带有适配器的简单视图寻呼机,例如:
public class PagerNewProductAdapter extends FragmentStatePagerAdapter {
RealmList<SubCategory> categoryData;
String categoryName;
String categoryId;
Env env;
public PagerNewProductAdapter(Fragment fragment, RealmList<SubCategory> category, String categoryName, String categoryId, Env env) {
super(fragment.getChildFragmentManager());
categoryData = category;
this.categoryId = categoryId;
this.env = env;
this.categoryName = categoryName;
}
@Override
public int getCount() {
return categoryData.size() + 2;
}
@Override
public Fragment getItem(int position) {
Fragment fragment;
if (position == 0) {
fragment= ProductFragment.newInstance("2", categoryId, categoryName);
} else if (position == 1) {
fragment= ProductFragment.newInstance("3", categoryId, categoryName);
} else {
fragment= ProductFragment.newInstance("1", categoryData.get(position - 2).getCategoryId(), categoryName);
}
return fragment;
}
@Override
public CharSequence getPageTitle(int position) {
if (position == 0) {
// return env.appSubcategoryViewAll;
return env.appSubcategoryViewAll;
} else if (position == 1) {
return env.appDashboardCBrands;
} else {
return categoryData.get(position - 2).getName();
}
}
}
谢谢
【问题讨论】:
-
您可以将 Fragment 缓存在一个数组中。
-
怎么样,能解释一下
标签: java android android-fragments android-viewpager fragmentpageradapter