安卓Fragment资源懒加载
2016-04-12 17:33
357 查看
多个Fragment放到一个Activity里面,Activity启动时会同时加载所有Fragment资源,造成卡顿现象。实际情况我们想要的是只加载当前显示的Fragment资源即可,即懒加载模式,使用Fragment的setUserVisibleHint和getUserVisibleHint 。
下面是一段官方的描述:
* Set a hint to the system about whether this fragment's UI is currently visible
* to the user. This hint defaults to true and is persistent across fragment instance
* state save and restore.
*
*
<p>An app may set this to false to indicate that the fragment's UI is
* scrolled out of visibility or is otherwise not directly visible to the user.
* This may be used by the system to prioritize operations such as fragment lifecycle updates
* or loader ordering behavior.</p>
*
*
@param
isVisibleToUser
true if this fragment's UI is currently visible to the user (default),
* false if it is not.
public void
setUserVisibleHint(boolean
isVisibleToUser)
/**
*
@return The current value of the user-visible hint on this fragment.
*
@see
#setUserVisibleHint(
boolean)
*/
public boolean
getUserVisibleHint() {
return
mUserVisibleHint;
}
总结一句就是isVisibleToUser为Ture时此fragment可见,false时不可见
Fragment懒加载实现思路就是把Fragment的资源加载写到一个LazyLoad()函数里面,当fragment可见时对资源进行加载。示例代码如下:
在Fragement中重写setUserVisibleHint方法
@Override
public void
setUserVisibleHint(boolean
isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(getUserVisibleHint()){
mIsVisible
=
true
;
lazyLoad();
}else{
mIsVisible
=
false
;
}
}
lazyLoad 负责进行耗时操作,这里需要注意判断是否执行过Create,系统执行流程会先执行到setUserVisibleHint 然后执行onCreate最后再去执行setUserVisibleHint,初始化工作放到onCreate里面,加载资源时判断是否已经执行过onCreate方法,否则第一次执行 setUserVisibleHint 方法时会因为没有初始化报空指针异常。
private void lazyLoad(){
if(mIsVisible
&&getIsCrearte
()){
//网络请求,资源加载等费事操作
}
}
下面是一段官方的描述:
* Set a hint to the system about whether this fragment's UI is currently visible
* to the user. This hint defaults to true and is persistent across fragment instance
* state save and restore.
*
*
<p>An app may set this to false to indicate that the fragment's UI is
* scrolled out of visibility or is otherwise not directly visible to the user.
* This may be used by the system to prioritize operations such as fragment lifecycle updates
* or loader ordering behavior.</p>
*
*
@param
isVisibleToUser
true if this fragment's UI is currently visible to the user (default),
* false if it is not.
public void
setUserVisibleHint(boolean
isVisibleToUser)
/**
*
@return The current value of the user-visible hint on this fragment.
*
@see
#setUserVisibleHint(
boolean)
*/
public boolean
getUserVisibleHint() {
return
mUserVisibleHint;
}
总结一句就是isVisibleToUser为Ture时此fragment可见,false时不可见
Fragment懒加载实现思路就是把Fragment的资源加载写到一个LazyLoad()函数里面,当fragment可见时对资源进行加载。示例代码如下:
在Fragement中重写setUserVisibleHint方法
@Override
public void
setUserVisibleHint(boolean
isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(getUserVisibleHint()){
mIsVisible
=
true
;
lazyLoad();
}else{
mIsVisible
=
false
;
}
}
lazyLoad 负责进行耗时操作,这里需要注意判断是否执行过Create,系统执行流程会先执行到setUserVisibleHint 然后执行onCreate最后再去执行setUserVisibleHint,初始化工作放到onCreate里面,加载资源时判断是否已经执行过onCreate方法,否则第一次执行 setUserVisibleHint 方法时会因为没有初始化报空指针异常。
private void lazyLoad(){
if(mIsVisible
&&getIsCrearte
()){
//网络请求,资源加载等费事操作
}
}
相关文章推荐
- 彭博士谈伤精与养精~ 中风的原因有哪些?|
- 为新手准备的 Codea 着色器(Shader)教程
- Hdu 2089-不要62 数位dp
- java动态代理(JDK和cglib)
- 作业
- 独立模式的Hadoop环境搭建
- Asp.net mvc ViewBag和ViewData比较
- etcd的使用
- 《超实用的Node.js代码段》连载一:获取Buffer对象字节长度
- 博世传感器调试笔记(一)----加速度传感器BMA253
- Android USB Host与HID通讯
- win10如何查看激活信息
- C++第三次实验-项目2
- Java IO最详解
- lua-5.1 和 lua-5.2 之间的一些差异
- 可怕!为什么有些人喜欢吃阳气,有些人喜欢吃阴气?
- xshell工具或Linux系统中运行jar文件
- 【ping】测试ip地址是否能访问
- 【route】管理本地路由信息
- 学习推荐书籍--程序员