blackHawk
blackHawk

Reputation: 6307

How to make viewpager to not load pages again if they are already loaded

I have Viewpager with 7 tabs, like in play store if we tap on tab on main screen it loads and if we tap that tab again it does not, I want if i tap on tab 1 it should load then i lets tap on tab 7 and then i tap tab 1 again it should not load, and only load if some conditions met, how can i do this? right now its just simple view pager with adapter like:

  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();
        }
     }
  }

Thanks

Upvotes: 3

Views: 1317

Answers (4)

Tushar Lathiya
Tushar Lathiya

Reputation: 1038

I think you need to use Fragmentation. it is working as per your requirements. If you have any query please let me know.

Step 1: Implement Fragmentation https://github.com/YoKeyword/Fragmentation

Step 2: extents your activity from SupportActivity instead of AppCompatActivity extents your fragment from SupportFragment instead of Fragment

Step 3: Remove onresume and onpause method from your fragment

Step 4: call onPause(); in onViewCreated method

@Override
    public void onSupportVisible() {
        super.onSupportVisible();
       onResume();
        if(!isLoaded){
            isLoaded = true;
//Load your data here 
}
}
@Override
    public void onSupportInvisible() {
        super.onSupportInvisible();
       onPause();
    }

Step 5: set setlimit viewPager.setOffscreenPageLimit(numberi);

Happy Coding.

Upvotes: 0

Pooja
Pooja

Reputation: 485

Simply add setOffscreenPageLimit like this:

    viewPager = findViewById(R.id.viewpager);
    viewPager.setOffscreenPageLimit(3);

3 = Number of fragments inside viewpager.

Upvotes: 2

Enzokie
Enzokie

Reputation: 7415

You can store the previously created Fragment in an Array. This ensures that we will use the previously created instance if it exists in our Array.

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;
      }
      ...
  }

Upvotes: -1

SergeyBukarev
SergeyBukarev

Reputation: 1608

I think in this case you can use 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();
    }
  }
}

Upvotes: 0

Related Questions