ViewPager部分源码分析一:加载数据
onMeasure()调用populate(),完成首次数据初始化。
populate()维护ViewPager的page,包括mItems和mAdapter。
populate():
if (curItem == null && N > 0) {
curItem = addNewItem(mCurItem, curIndex);
}
似乎在首次数据初始化时会用到。
if (curItem != null) {
float extraWidthLeft = 0.f;
int itemIndex = curIndex - 1;
ItemInfo ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
final int clientWidth = getClientWidth();
final float leftWidthNeeded = clientWidth <= 0 ? 0 :
2.f - curItem.widthFactor + (float) getPaddingLeft() / (float) clientWidth;
for (int pos = mCurItem - 1; pos >= 0; pos--) {
if (extraWidthLeft >= leftWidthNeeded && pos < startPos) {
if (ii == null) {
break;
}
if (pos == ii.position && !ii.scrolling) {
mItems.remove(itemIndex);
mAdapter.destroyItem(this, pos, ii.object);
if (DEBUG) {
Log.i(TAG, "populate() - destroyItem() with pos: " + pos +
" view: " + ((View) ii.object));
}
itemIndex--;
curIndex--;
ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
}
} else if (ii != null && pos == ii.position) {
extraWidthLeft += ii.widthFactor;
itemIndex--;
ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
} else {
ii = addNewItem(pos, itemIndex + 1);
extraWidthLeft += ii.widthFactor;
curIndex++;
ii = itemIndex >= 0 ? mItems.get(itemIndex) : null;
}
}
float extraWidthRight = curItem.widthFactor;
itemIndex = curIndex + 1;
if (extraWidthRight < 2.f) {
ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
final float rightWidthNeeded = clientWidth <= 0 ? 0 :
(float) getPaddingRight() / (float) clientWidth + 2.f;
for (int pos = mCurItem + 1; pos < N; pos++) {
if (extraWidthRight >= rightWidthNeeded && pos > endPos) {
if (ii == null) {
break;
}
if (pos == ii.position && !ii.scrolling) {
mItems.remove(itemIndex);
mAdapter.destroyItem(this, pos, ii.object);
if (DEBUG) {
Log.i(TAG, "populate() - destroyItem() with pos: " + pos +
" view: " + ((View) ii.object));
}
ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
}
} else if (ii != null && pos == ii.position) {
extraWidthRight += ii.widthFactor;
itemIndex++;
ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
} else {
ii = addNewItem(pos, itemIndex);
itemIndex++;
extraWidthRight += ii.widthFactor;
ii = itemIndex < mItems.size() ? mItems.get(itemIndex) : null;
}
}
}
calculatePageOffsets(curItem, curIndex, oldCurInfo);
}
第一个for循环实现当前page左边的page处理;当向左滑时,销毁左边的page;当向右滑时,增加左边的page。
第二个for循环实现当前page右边的page处理;当向左滑时,增加右边的page;当向右滑时,销毁右边的page。
即在ViewPager中始终保留的是3个page:左边,当前,右边。(没有override PagerAdapter.getPageWidth()方法的情形下!跟ViewPager的padding也有关系)
其中的两次增加page的情况:
onTouchEvent() --> ViewCompat.postInvalidateOnAnimation() --> BaseViewCompatImpl.postInvalidateOnAnimation() --> View.invalidate()
--> ..这一步也不知道是什么.. --> View.updateDisplayListIfDirty --> View.computeScroll() --> ViewPager.completeScroll() --> ViewCompat.postOnAnimation()
--> BaseViewCompatImpl.postOnAnimation() --> View.postDelayed() --> Handler.getPostMessage() --> 后面就是Handler对消息的处理了。
private static Message getPostMessage(Runnable r) {
Message m = Message.obtain();
m.callback = r;
return m;
}
原来Messge里有一个callback,是Runnable类型的,Handler.handleCallback()在处理消息时就是:
private static void handleCallback(Message message) {
message.callback.run();
}
直接调用callback的run()方法。
这么一大串的事情,其实两次增加page的情况就是:ViewPager的mEndScrollRunnable最终在Handler.handleCallback()完成的。
private final Runnable mEndScrollRunnable = new Runnable() {
public void run() {
setScrollState(SCROLL_STATE_IDLE);
populate();
}
};
看到run()方法中的populate()方法了么!!
ViewPager部分源码分析一:加载数据的更多相关文章
- Spring Boot源码分析-配置文件加载原理
在Spring Boot源码分析-启动过程中我们进行了启动源码的分析,大致了解了整个Spring Boot的启动过程,具体细节这里不再赘述,感兴趣的同学可以自行阅读.今天让我们继续阅读源码,了解配置文 ...
- mybatis源码分析--如何加载配置及初始化
简介 Mybatis 是一个持久层框架,它对 JDBC 进行了高级封装,使我们的代码中不会出现任何的 JDBC 代码,另外,它还通过 xml 或注解的方式将 sql 从 DAO/Repository ...
- 精尽Spring Boot源码分析 - 配置加载
该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...
- Spring源码分析之-加载IOC容器
本文接上一篇文章 SpringIOC 源码,控制反转前的处理(https://mp.weixin.qq.com/s/9RbVP2ZQVx9-vKngqndW1w) 继续进行下面的分析 首先贴出 Spr ...
- 第一次源码分析: 图片加载框架Picasso源码分析
使用: Picasso.with(this) .load("http://imgstore.cdn.sogou.com/app/a/100540002/467502.jpg") . ...
- 源码分析: 图片加载框架Picasso源码分析
使用: Picasso.with(this) .load("http://imgstore.cdn.sogou.com/app/a/100540002/467502.jpg") . ...
- 1.Sentinel源码分析—FlowRuleManager加载规则做了什么?
最近我很好奇在RPC中限流熔断降级要怎么做,hystrix已经1年多没有更新了,感觉要被遗弃的感觉,那么我就把眼光聚焦到了阿里的Sentinel,顺便学习一下阿里的源代码. 这一章我主要讲的是Flow ...
- springboot集成mybatis源码分析-启动加载mybatis过程(二)
1.springboot项目最核心的就是自动加载配置,该功能则依赖的是一个注解@SpringBootApplication中的@EnableAutoConfiguration 2.EnableAuto ...
- JDBC源码分析(加载过程)
public static void main(String[] args) { String url = "jdbc:mysql://172.16.27.11:3306/jdbcT ...
- Mybatis源码解析(二) —— 加载 Configuration
Mybatis源码解析(二) -- 加载 Configuration 正如上文所看到的 Configuration 对象保存了所有Mybatis的配置信息,也就是说mybatis-config. ...
随机推荐
- 实现简易而强大的游戏AI——FSM,有限状态机
http://blog.friskit.me/2012/05/introduction-of-fsm/ 在很久很久以前,受限于计算机性能和图形效果,游戏往往是以玩家为唯一主动对象的,玩家发出动作,游戏 ...
- hiho #1372:平方求 (bfs)
#1372 : 平方求和 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 对于一个非负整数n,最少需要几个完全平方数,使其和为n? 输入 输入包含多组数据.对于每组数据: ...
- BZOJ 3365: [Usaco2004 Feb]Distance Statistics 路程统计
Description 一棵树,统计距离不大于 \(k\) 的点对个数. Sol 点分治. 发现自己快把点分治忘干净了... 找重心使所有儿子的最大值尽量小,然后每次处理全部子树,再减去每个子树的贡献 ...
- BZOJ 3942: [Usaco2015 Feb]Censoring
Description 有两个字符串,每次用一个中取出下一位,放在一个字符串中,如果当前字符串的后缀是另一个字符串就删除. Sol KMP+栈. 用一个栈来维护新加的字符串就可以了.. 一开始我非常的 ...
- Python文件操作题
1.如何用Python删除一个文件 os.remove(filename)或者os.unlink(filename). 2.Python如何copy一个文件 shutil模块里有一个copyfile函 ...
- 大数据热点问题TOP K
1单节点上的topK (1)批量数据 数据结构:HashMap, PriorityQueue 步骤:(1)数据预处理:遍历整个数据集,hash表记录词频 (2)构建最小堆:最小堆只存k个数据. 时间复 ...
- 【GoLang】与或非 异或操作
在Go规范,^这个运算符在一元运算符和二元运算符中都出现了.那么他们分别是啥?在规范中说道:^ bitwise XOR integers这是按位异或. Go语言位操作实例 <!-- lang: ...
- [20160731][转]JAVA当中变量什么时候需要初始化
1. 对于类的成员变量,不管程序有没有显式的进行初始化,Java虚拟机都会先自动给它初始化为默认值. 默认值如下: Boolean false ...
- SpringMVC中Controller跳转到另一个Controller方法
1.直接Redirect后加 Controller/Action Response.Redirect("/User/Edit"); return Redirect("/U ...
- mysql性能优化-简易版
mysql性能优化 sql语句优化 如何发现有问题的sql? 开启mysql慢查询 show variables like 'slow_query_log' set global slow_query ...