Android中ViewPager+Fragment禁止预加载问题
使用ViewPager+Fragment组合加载数据,由于ViewPager的内部机制所限制,会默认至少预加载一个。但有时我们必须禁止预加载,比如第1个和第2个页面可以任意浏览,第3个页面需要登录才能浏览,此时会导致在切换到第2个页面时,便跳转到登录页面。
ViewPager.setOffscreenPageLimit(0)
可不可以设置ViewPager的预加载为0,不就解决问题了吗?经过测试发现,即使设置vp.setOffscreenPageLimit(0)
,也会被强制拽回1!
查看Viewpager的setOffscreenPageLimit()源码:
private static final int DEFAULT_OFFSCREEN_PAGES = 1;
/**
* 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.
*
* <p>This is offered as an optimization. If you know in advance the number
* of pages you will need to support or have lazy-loading mechanisms in place
* on your pages, tweaking this setting can have benefits in perceived smoothness
* of paging animations and interaction. If you have a small number of pages (3-4)
* that you can keep active all at once, less time will be spent in layout for
* newly created view subtrees as the user pages back and forth.</p>
*
* <p>You should keep this limit low, especially if your pages have complex layouts.
* This setting defaults to 1.</p>
*
* @param limit How many pages will be kept offscreen in an idle state.
*/
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();
}
}
我们发现,即使设置为0,还是会在判断后设为默认值1。
Fragment.setUserVisibleHint(boolean isVisibleToUser)
Fragment中有一个setUserVisibleHint(boolean isVisibleToUser)
方法,这个方法就是告诉用户,UI对用户是否可见,那么我们在这里去加载数据会怎么样呢?
因为ViewPager会加载好多Fragment,为了节省内容等会在Fragment不可见的某个时候调用onDestroyView()将用户界面销毁掉,但是Fragment的实例还在,所以可能第一次加载没有问题,但是再次回到第一个Fragment去加载时就会出现UI对用户可见但是视图还没有初始化的现象。
最终解决方案LazyLoadFragment
LazyLoadFragment.java
/**
* Fragment预加载问题的解决方案:
* 1.可以懒加载的Fragment
* 2.切换到其他页面时停止加载数据(可选)
*/
public abstract class LazyLoadFragment extends Fragment {
/**
* 视图是否已经初初始化
*/
protected boolean isInit = false;
protected boolean isLoad = false;
protected final String TAG = "LazyLoadFragment";
private View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(setContentView(), container, false);
isInit = true;
/**初始化的时候去加载数据**/
isCanLoadData();
return view;
}
/**
* 视图是否已经对用户可见,系统的方法
*/
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
isCanLoadData();
}
/**
* 是否可以加载数据
* 可以加载数据的条件:
* 1.视图已经初始化
* 2.视图对用户可见
*/
private void isCanLoadData() {
if (!isInit) {
return;
}
if (getUserVisibleHint()) {
lazyLoad();
isLoad = true;
} else {
if (isLoad) {
stopLoad();
}
}
}
/**
* 视图销毁的时候讲Fragment是否初始化的状态变为false
*/
@Override
public void onDestroyView() {
super.onDestroyView();
isInit = false;
isLoad = false;
}
/**
* 设置Fragment要显示的布局
*
* @return 布局的layoutId
*/
protected abstract int setContentView();
/**
* 获取设置的布局
*
* @return
*/
protected View getContentView() {
return view;
}
/**
* 找出对应的控件
*
* @param id
* @param <T>
* @return
*/
protected <T extends View> T findViewById(int id) {
return (T) getContentView().findViewById(id);
}
/**
* 当视图初始化并且对用户可见的时候去真正的加载数据
*/
protected abstract void lazyLoad();
/**
* 当视图已经对用户不可见并且加载过数据,如果需要在切换到其他页面时停止加载数据,可以调用此方法
*/
protected void stopLoad() {
}
}
Fragment1.java
public class Fragment1 extends LazyLoadFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.i(TAG, "Fragment1: onCreateView");
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
Log.i(TAG, "Fragment1: setUserVisibleHint(" + isVisibleToUser + ")");
super.setUserVisibleHint(isVisibleToUser);
}
@Override
public int setContentView() {
return R.layout.fragment_1;
}
@Override
protected void lazyLoad() {
Log.d(TAG, "Fragment1: lazyLoad");
}
@Override
protected void stopLoad() {
Log.d(TAG, "Fragment1: stopLoad");
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.vp);
initData();
}
private void initData() {
final List<Fragment> fragments = new ArrayList<>();
fragments.add(new Fragment1());
fragments.add(new Fragment2());
fragments.add(new Fragment3());
fragments.add(new Fragment4());
fragments.add(new Fragment5());
viewPager.setOffscreenPageLimit(0);
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
});
}
@Override
protected void onResume() {
super.onResume();
Log.d("yezhou", "MainActivity: onResume");
}
}
日志记录
App Strat -> Fragment1
Fragment1: setUserVisibleHint(false)
Fragment2: setUserVisibleHint(false)
Fragment1: setUserVisibleHint(true)
Fragment1: onCreateView
Fragment1: lazyLoad
Fragment2: onCreateView
Fragment1 -> Fragment2
Fragment3: setUserVisibleHint(false)
Fragment1: setUserVisibleHint(false)
Fragment1: stopLoad
Fragment2: setUserVisibleHint(true)
Fragment2: lazyLoad
Fragment3: onCreateView
Fragment2 -> Fragment3
Fragment4: setUserVisibleHint(false)
Fragment2: setUserVisibleHint(false)
Fragment2: stopLoad
setUserVisibleHint(true)
Fragment3: lazyLoad
Fragment4: onCreateView
Fragment3 -> Fragment2
Fragment1: setUserVisibleHint(false)
Fragment3: setUserVisibleHint(false)
Fragment3: stopLoad
Fragment2: setUserVisibleHint(true)
Fragment2: lazyLoad
Fragment1: onCreateView
Activity onResume -> Fragment1
Fragment1: setUserVisibleHint(false)
Fragment2: setUserVisibleHint(false)
Fragment1: setUserVisibleHint(true)
Fragment1: onCreateView
Fragment1: lazyLoad
Fragment2: onCreateView
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/11/android-preventing-preloading-in-viewpager-fragment/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
共有 0 条评论