user6741232 user6741232 - 1 year ago 158
Android Question

How to disable preloading next page in a ViewPager?

How can I disable the preloading in a


I have a ViewPager with 3 pages. So I don't want to load the next page and previous page. How can I disable this behaviour?

I tried


But this is not working.

Answer Source

As doc says:

This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth.

So following code is not work as you expected:


But there is a trick, You can initilize view|value just when fragment is visible to user with:

    public void setMenuVisibility(final boolean visible) {
        if (visible) {
            // do something nice here