Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable the preloading in a viewpager?

How can I disable the preloading in a viewpager?.

I have a viewpager with 3 pages. So i dont want load the next page and previously page. How can i disable that behavior?

like image 637
Lucas Santos Avatar asked May 30 '14 16:05

Lucas Santos


People also ask

How to disable Preloading next page in ViewPager?

use this viewPager. setOffscreenPageLimit(1) instead of viewPager. setOffscreenPageLimit(0) . because the minimum value of setOffscreenPageLimit() is 1.

How do I turn off animations in ViewPager?

I finally found out: the issue can be solved by just calling the mViewPager. setCurrentItem(position) with an extra parameter to false , which is the smooth scroll for the ViewPager . After this, the scroll will be done without any smoothing and thus the animations won't be seen.

How do I stop ViewPager from scrolling?

A simple solution is to create your own subclass of ViewPager that has a private boolean flag, isPagingEnabled . Then override the onTouchEvent and onInterceptTouchEvent methods. If isPagingEnabled equals true invoke the super method, otherwise return .

How to set ViewPager in android?

To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .


2 Answers

How can I disable the preloading in a ViewPager?.

it is not possible. ViewPager preloads always at least 1 page. If you don't want this behaviour you should not use the ViewPager. You could use, for instance, a RecyclerView

Old answer

you have to call setOffscreenPageLimit(1)

From the doc

Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.

like image 165
Blackbelt Avatar answered Oct 25 '22 23:10

Blackbelt


Well, it seems like it's a bit more complicated than one would think. The code below is an abstract class that should be implemented by your fragment. In fact it requires the following:

  1. Replace onActivityCreated with the lazyOnActivityCreated.
  2. Implement the lazyLoadData method that loads the data for the tab.
  3. After you have loaded the data or for any reason you have changed the active data call setTabDataId.

I know it sound a bit complicated but it works every time and will definitely work when setUserHint is no good.

Here is the abstract class:

public abstract class LazyFetchTabFragment extends AbstractTabFragment {

private String tabDataId;
private String activeTabDataId;

private boolean isActive = false;
private boolean isCreated = false;

@Override
public void onStop(){
    super.onStop();
    isCreated = false;
    activeTabDataId = null;
}

/**
 * An abstract method that should be called instead of {@link android.app.Fragment#onActivityCreated(Bundle)}.
 */
public abstract void lazyOnActivityCreated() throws Exception;
public abstract void lazyLoadData() throws Exception;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    try {
        lazyOnActivityCreated();
        isCreated = true;
        loadData();
    } catch (Exception e) {
        Log.e(this.getClass().getCanonicalName() + " - "
                + Thread.currentThread().getStackTrace()[2].getMethodName(), e.toString());
    }
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    // When changing tabs the spinner setOnItemSelectedListener is not called,
    // so use code below to trigger loading data.
    isActive = isVisibleToUser;
    loadData();
}

private void loadData(boolean force) {
    if (isActive && isCreated && tabDataId != null &&
            (force || (activeTabDataId == null || !activeTabDataId.equals(tabDataId))))
        try {
            lazyLoadData();
            activeTabDataId = tabDataId;
        } catch (Exception ex){
            Log.e(this.getClass().getCanonicalName() + " - "
                    + Thread.currentThread().getStackTrace()[2].getMethodName(), ex.toString());
        }
}

public void setTabDataId(String tabDataId) {
    this.tabDataId = tabDataId;
}

Good luck!

like image 25
ichalos Avatar answered Oct 25 '22 23:10

ichalos