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部分源码分析一:加载数据的更多相关文章

  1. Spring Boot源码分析-配置文件加载原理

    在Spring Boot源码分析-启动过程中我们进行了启动源码的分析,大致了解了整个Spring Boot的启动过程,具体细节这里不再赘述,感兴趣的同学可以自行阅读.今天让我们继续阅读源码,了解配置文 ...

  2. mybatis源码分析--如何加载配置及初始化

    简介 Mybatis 是一个持久层框架,它对 JDBC 进行了高级封装,使我们的代码中不会出现任何的 JDBC 代码,另外,它还通过 xml 或注解的方式将 sql 从 DAO/Repository ...

  3. 精尽Spring Boot源码分析 - 配置加载

    该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...

  4. Spring源码分析之-加载IOC容器

    本文接上一篇文章 SpringIOC 源码,控制反转前的处理(https://mp.weixin.qq.com/s/9RbVP2ZQVx9-vKngqndW1w) 继续进行下面的分析 首先贴出 Spr ...

  5. 第一次源码分析: 图片加载框架Picasso源码分析

    使用: Picasso.with(this) .load("http://imgstore.cdn.sogou.com/app/a/100540002/467502.jpg") . ...

  6. 源码分析: 图片加载框架Picasso源码分析

    使用: Picasso.with(this) .load("http://imgstore.cdn.sogou.com/app/a/100540002/467502.jpg") . ...

  7. 1.Sentinel源码分析—FlowRuleManager加载规则做了什么?

    最近我很好奇在RPC中限流熔断降级要怎么做,hystrix已经1年多没有更新了,感觉要被遗弃的感觉,那么我就把眼光聚焦到了阿里的Sentinel,顺便学习一下阿里的源代码. 这一章我主要讲的是Flow ...

  8. springboot集成mybatis源码分析-启动加载mybatis过程(二)

    1.springboot项目最核心的就是自动加载配置,该功能则依赖的是一个注解@SpringBootApplication中的@EnableAutoConfiguration 2.EnableAuto ...

  9. JDBC源码分析(加载过程)

    public static void main(String[] args) {     String url = "jdbc:mysql://172.16.27.11:3306/jdbcT ...

  10. Mybatis源码解析(二) —— 加载 Configuration

    Mybatis源码解析(二) -- 加载 Configuration    正如上文所看到的 Configuration 对象保存了所有Mybatis的配置信息,也就是说mybatis-config. ...

随机推荐

  1. HDU 5120 A Curious Matt(2014北京赛区现场赛A题 简单模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5112 解题报告:扫一遍 #include<cstdio> #include<cstr ...

  2. iOS开发——网络篇——数据安全(MD5),HTTPS,检测网络状态

    一.数据安全 1.提交用户的隐私数据一定要使用POST请求提交用户的隐私数据GET请求的所有参数都直接暴露在URL中请求的URL一般会记录在服务器的访问日志中服务器的访问日志是黑客攻击的重点对象之一 ...

  3. 十进制转为N进制

    昨天笔试遇到的题,如果是正数,不断以余数做除法即可: void convert(int num, int N, vector<char>& data) { int number = ...

  4. Android Studio日志工具的使用

    Android Studio的LogCat工具 Verbose:对应Log.v(),这个方法用于打印那些最为琐碎的信息,意义最小的日志信息.是Android日志里面级别最低的一种. Debug:对应L ...

  5. 将Apache加入到linux系统service

    将Apache加入到linux系统service 将apache加入到linux系统服务,用service命令来控制apache的启动和停止. 本文由乌合之众瞎写http://www.cnblogs. ...

  6. linux——基本配置

    环境:Ubuntu 12.04.2 LTS (GNU/Linux 3.5.0-23-generic i686) 网络配置 #临时改变 #修改IP和子网掩码 sudo ifconfig eth0 192 ...

  7. shell脚本实现拷贝大文件显示百分比的代码分享

    #!/bin/sh strace -q -eread cp -- "${1}" "${2}" 2>&1 \| awk '{    count += ...

  8. ITIL与ITSM的联系与区别

    1.ITIL(IT Infrastructure Library)是CCTA(英国国家计算机和电信局)于20世纪80年代末开发的一套IT服务管理标准库,它把英国各个行业在IT管理方面的最佳实践归纳起来 ...

  9. centos7精简版(minimal)killall: command not found

    centos7精简版(minimal)运行killall命令提示 command not found 是由于没有安装psmisc所致 Psmisc软件包包含三个帮助管理/proc目录的程序. 安装下列 ...

  10. Zlib 在windows上的编译

    1.下载http://www.zlib.net 下载,最新版本1.2.8 2.解压后,实际已提供了在vc下编译的工程,目录为:zlib-1.2.8\contrib\vstudio. 其中的zlibst ...