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?
use this viewPager. setOffscreenPageLimit(1) instead of viewPager. setOffscreenPageLimit(0) . because the minimum value of setOffscreenPageLimit() is 1.
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.
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 .
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 .
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.
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:
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With