Skip to content Skip to sidebar Skip to footer

Stop Android Tablayout Loading Next Page Automatically

I've realized when using a tab layout in Android it always loads the tabs touching it, i.e. the tab before and the tab after so it is loaded when you page to it. However, I load l

Solution 1:

By default it is viewpager.setOffscreenPageLimit(1) , meaning View pager will by default load atleast 1 on the right and one on the left tab of current tab. It is done so, mostly because there is a point when u slide viewpager, when certain area of both tabs is visible. For those smooth transitions preloading is required. You cannot set it viewpager.setOffscreenPageLimit(0). The only way out is to use this method setUserVisibleHint add this to your fragment

@OverridepublicvoidsetUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        // load data here
    }else{
       // fragment is no longer visible
    }
}

This will be called only when that particular tab is visible to user, so only then u can call all loadfing function. Hope it helps.

Post a Comment for "Stop Android Tablayout Loading Next Page Automatically"