Essam Goda Essam Goda - 19 days ago 8
Android Question

I want to disable preloading next page in a ViewPager

How can I disable the preloading in a ViewPager?

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

I tried


viewPager.setOffscreenPageLimit(1)


But this is not working.

and tried

@Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if (visible) {
// do something nice here
}
}


but RecyclerView which in next fragment show this


RecyclerView: No adapter attached; skipping layout


apologize for my English

Answer

The minimum value for the off screen limit is 1. what you can do is, handle it yourself using the "setUserVisibleHint" method in your fragments, like for instance.

public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser)
           // execute your data loading logic.
}