我有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();
}
}
}
谢谢
答案 0 :(得分:0)
我认为在这种情况下,您可以使用SparseArray
public class PagerNewProductAdapter extends FragmentStatePagerAdapter {
private final SparseArray<Fragment> registeredFragments = new SparseArray<>();
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;
}
@NonNull
@Override
public Object instantiateItem(ViewGroup container, int position) {
if (registeredFragments.get(position) == null) {
Object fragment = super.instantiateItem(container, position);
registeredFragments.put(position, (BaseFragment) fragment);
return fragment;
} else {
return registeredFragments.get(position);
}
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
registeredFragments.remove(position);
}
@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 int getCount() {
return categoryData.size() + 2;
}
@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();
}
}
}
答案 1 :(得分:0)
像这样简单地添加setOffscreenPageLimit:
viewPager = findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(3);
3 = viewpager中的片段数。
答案 2 :(得分:0)
我认为您需要使用Fragmentation。它按照您的要求工作。如果您有任何疑问,请告诉我。
步骤1: 实施碎片化https://github.com/YoKeyword/Fragmentation
步骤2: 从SupportActivity而不是AppCompatActivity扩展您的活动 从SupportFragment扩展片段而不是Fragment
步骤3: 从片段中删除onresume和onpause方法
第4步: 调用onPause();在onViewCreated方法中
@Override
public void onSupportVisible() {
super.onSupportVisible();
onResume();
if(!isLoaded){
isLoaded = true;
//Load your data here
}
}
@Override
public void onSupportInvisible() {
super.onSupportInvisible();
onPause();
}
第5步: 设置setlimit viewPager.setOffscreenPageLimit(numberi);
快乐编码。
答案 3 :(得分:-1)
您可以将先前创建的片段存储在数组中。这样可以确保如果数组中存在以前创建的实例,我们将使用它。
public class PagerNewProductAdapter extends FragmentStatePagerAdapter {
Fragment[] cache;
RealmList<SubCategory> categoryData;
...
public PagerNewProductAdapter(Fragment fragment, RealmList<SubCategory> category, String categoryName, String categoryId, Env env) {
...
this.categoryName = categoryName;
this.cache = new Fragment[getCount()];
}
@Override
public int getCount() {
return categoryData.size() + 2;
}
@Override
public Fragment getItem(int position) {
Fragment cachedFragment = cache[position]
if(cachedFragment == null){
if (position == 0) {
cache[position] = ProductFragment.newInstance("2", categoryId, categoryName);
cachedFragment = cache[position];
} else if (position == 1) {
cache[position] = ProductFragment.newInstance("3", categoryId, categoryName);
cachedFragment = cache[position];
} else {
cache[position]= ProductFragment.newInstance("1", categoryData.get(position - 2).getCategoryId(), categoryName);
cachedFragment = cache[position];
}
}
return cachedFragment;
}
...
}