1、一般我們預加載的時候,都會指定顯示的數量,比如viewPager2.setOffscreenPageLimit(1);
如果設置 setOffscreenPageLimit(1), 會如何處理呢?
經過測試,會加載第一頁與第二頁,如果切換到第二頁,會加載第三頁,但不會執行onResume方法,也就是說只要切換到當前的頁才會執行onResume方法。
FileImageFragment onAttach FileImageFragment onViewCreated FileImageFragment onStart FileImageFragment onResume --->只有當前頁里的fragment會執行。 FileVideoFragment onAttach FileVideoFragment onViewCreated FileVideoFragment onStart
2、如果設置為0,會咱樣呢?直接崩了,並且會有提示一定要大於等於1
3、如果處理成懶加載的話,其實很簡單,只要是這個方法setOffscreenPageLimit不去設置,就可以了。
一開始本來想處理onResume方法,來進行懶加載,其實並不需要,只要不調用setOffscreenPageLimit這個方法即可。
注:
加載是在
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
......這里是加載
}
但是如果page數量超過3的時候,自將第一個,與第五個,銷毀,然后再創建。
解決:
setOffscreenPageLimit(2)這樣就可以了。