You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
By clicking “Sign up for GitHub”, you agree to our
terms of service
and
privacy statement
. We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
setUserVisibleHint是必须在Vp里面 onhiddenchange是show hiden方式的时候调用、
但是以上两种方式实际上都在慢慢被砍掉 特别是前者 在比较新的版本中已经被标记废弃了。
建议使用VP2 (好像Vp1的adapter的第二个Int参数也行)或者单一的show hidden配合setmaxlifecycle,可以控制fragment的生命周期。
建议百度搜索“setmaxlifecycle fragment懒加载”,进行详细了解
本来Fragment的 onResume()表示的是当前Fragment处于可见且可交互状态,但由于ViewPager的缓存机制,它已经失去了意义,也就是说我们只是打开了“福利”这个Fragment,但其实“休息视频”和“拓展资源”这两个Fragment的数据也都已经加载好了。
Fragment里setUserVisibleHint方法
在这里判断 是否可见 是否第一次加载
还有一种方法 在viewpager2 中设置 setMaxLifeCycler(START)
使得预加载后的fragment 最多生命周期走到start
就可以在onresume中去做网络请求
本来Fragment的 onResume()表示的是当前Fragment处于可见且可交互状态,但由于ViewPager的缓存机制,它已经失去了意义,也就是说我们只是打开了“福利”这个Fragment,但其实“休息视频”和“拓展资源”这两个Fragment的数据也都已经加载好了。
Fragment里setUserVisibleHint方法
在这里判断 是否可见 是否第一次加载
还有一种方法 在viewpager2 中设置 setMaxLifeCycler(START)
使得预加载后的fragment 最多生命周期走到start
就可以在onresume中去做网络请求
AndroidX下,FragmentPagerAdapter构造函数可传递behavior参数,原理上使用了setMaxLifeCycler,修复了以上onResume回调的问题
liyanfei250, rambowding, chen3441251, senlinxuefeng, and wyvector reacted with thumbs up emoji
senlinxuefeng reacted with hooray emoji
All reactions