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?
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?
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!
onCreateView
. Can you handle that?? –
Ory 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();
}
}
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
}
}
© 2022 - 2024 — McMap. All rights reserved.