How can I disable the preloading in a viewpager?
Asked Answered
H

4

18

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?

Howlet answered 30/5, 2014 at 16:23 Comment(1)
Good question! I also want to know if it's possible to load only one page at first and then cache other loaded pages.Donner
I
11

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.

Interrupter answered 30/5, 2014 at 16:26 Comment(3)
Thank you! =D But OffscreenPageLimit(0) don't work because the minimum value of the parameters is 1. OffscreenPageLimit(0) throws a log in the logcatHowlet
I hope this is because the 1 page is the actual page on display. It's sad that I need to do this because android keeps changing GUI elements on the wrong page and I'm using fragments in a ViewPager so everything has to be static to avoid OutOfMemory. I hope Android just throws out the entire GUI library at some point and starts over from scratch, everything is just awful now. I have an 800 line fragment now to be able to display maybe 15 dynamic elements.Commination
Solution provided does not answer question. The original question is how to disable preloading in a ViewPager (i.e. do not load next or previous page). The solution provided does nothing as it assigns the default value of 1.Nevertheless
B
7

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!

Borreri answered 1/11, 2015 at 13:26 Comment(2)
This methods gives NPE because sometimes it get's called before the onCreateView. Can you handle that??Ory
I had to fully replace the answer, as it was not working properly. It will work properly now. I know it needs some polishing, but will probably help you for now!Borreri
O
1

I modify the ViewPager source code

private static final int DEFAULT_OFFSCREEN_PAGES = 0;

You can find the source in ViewPager about setOffscreenPageLimit

public void setOffscreenPageLimit(int limit) {
    if (limit < DEFAULT_OFFSCREEN_PAGES) {
        Log.w(TAG, "Requested offscreen page limit " + limit + " too small; defaulting to " +
                DEFAULT_OFFSCREEN_PAGES);
        limit = DEFAULT_OFFSCREEN_PAGES;
    }
    if (limit != mOffscreenPageLimit) {
        mOffscreenPageLimit = limit;
        populate();
    }
}
Organ answered 15/4, 2015 at 13:55 Comment(2)
Change the source code of what? The API itself? And how will it work when installed to a device that has the original untouched API?Borreri
@Borreri Usually using this approach you basically create ModifiedViewPager which you further use instead of ViewPager - and it works fine. Modification suggested by does not work though.Dermot
C
0

You can keep track of the current page with the help of ViewModel and liveData or any reactive api of your choice like this in onPageChangeCallback:

myViewPager.registerOnPageChangeCallback(ViewPager2.OnPageChangeCallback() {
    override fun onPageSelected(position: Int) {
    super.onPageSelected(position)
    viewModel.setCurrentPage(position) // a function in your viewmodel to keep track of current page
  })

In onViewCreated or onResume observe the current page and execute your code only when in certain page:

viewModel.currentPageLiveData.distinctUntilChanged().observe(viewLifecycleOwner) { currentPage ->
   if(currentPage == desiredPage){
         // excecute your code here
   }
}
Concurrent answered 1/6 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.