解决viewpager加载多个fragment 设置预加载的方法失效问题 引入fragment懒加载
只有当页面显示的时候才被加载
主要使用到的方法:setUserVisibleHint()、userVisibleHint,界面显示到不显示,以及不显示到显示都会执行setUserVisibleHint方法
abstract class LazyFragment: Fragment(){
private var isPrepare:Boolean =false
private var isLoaded:Boolean =false
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
isPrepare=true
lazyLoad()
}
override fun setUserVisibleHint(isVisibleToUser: Boolean) {
super.setUserVisibleHint(isVisibleToUser)
lazyLoad()
}
private fun lazyLoad() {
if(userVisibleHint&&isPrepare&&!isLoaded){
onLazyLoad()
isLoaded=true
}
}
abstract fun onLazyLoad()
}
来源:https://blog.csdn.net/wj_12113114/article/details/102778394