我的CSDN博客同步發(fā)布:ViewPager源碼分析(1):onMeasure橙数、onLayout
轉(zhuǎn)載請注明出處:【huachao1001的簡書:http://www.reibang.com/users/0a7e42698e4b/latest_articles】
在網(wǎng)上看了一些面經(jīng)荸型,感覺ViewPager被問到的概率還是蠻大的投储,于是決定去好好研究ViewPager源碼衡招,一步一步去琢磨ViewPager的實現(xiàn)融师,并寫到博文里來~。
我們知道蚁吝,ViewPager是一個ViewGroup旱爆,而我們平時自己自定義ViewGroup時,除了至少寫兩個構(gòu)造函數(shù)以外窘茁,onMeasure和onLayout這兩個函數(shù)基本上是必須要去寫的怀伦。今天先把onMeasure和onLayout兩個函數(shù)仔細研究研究~
onMeasure
ViewPager將子View分為兩種,一種是Decor View 用于裝飾ViewPager山林,它可能需要占用一些空間房待;另一種是普通的子View,也就是我們橫滑時顯示的各個View驼抹。onMeasure首先是對Decor View進行測量桑孩,然后再對普通的子View進行測量。詳細的注釋如下:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//根據(jù)布局文件框冀,設(shè)置尺寸信息流椒,默認大小為0
setMeasuredDimension(getDefaultSize(0, widthMeasureSpec),
getDefaultSize(0, heightMeasureSpec));
final int measuredWidth = getMeasuredWidth();
final int maxGutterSize = measuredWidth / 10;
//設(shè)置mGutterSize的值,后面再講mGutterSize
mGutterSize = Math.min(maxGutterSize, mDefaultGutterSize);
// ViewPager的顯示區(qū)域只能顯示對于一個View
//childWidthSize和childHeightSize為一個View的可用寬高大小
//即去除了ViewPager內(nèi)邊距后的寬高
int childWidthSize = measuredWidth - getPaddingLeft() - getPaddingRight();
int childHeightSize = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
//1.先對Decor View進行測量
//下面這個循環(huán)是只針對Decor View的明也,即用于裝飾ViewPager的View
int size = getChildCount();
for (int i = 0; i < size; ++i) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
//1.1 如果該View是Decor View,即用于裝飾ViewPager的View
if (lp != null && lp.isDecor) {
//1.2 獲取Decor View的在水平方向和豎直方向上的Gravity
final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
//1.3 默認Dedor View模式對應(yīng)的寬高是wrap_content
int widthMode = MeasureSpec.AT_MOST;
int heightMode = MeasureSpec.AT_MOST;
//1.4 記錄Decor View是在垂直方向上還是在水平方向上占用空間
boolean consumeVertical = vgrav == Gravity.TOP || vgrav == Gravity.BOTTOM;
boolean consumeHorizontal = hgrav == Gravity.LEFT || hgrav == Gravity.RIGHT;
//1.5 consumeHorizontal:如果是在垂直方向上占用空間宣虾,
// 那么水平方向就是match_parent,即EXACTLY
//而垂直方向上具體占用多少空間温数,還得由Decor View決定
//consumeHorizontal也是同理
if (consumeVertical) {
widthMode = MeasureSpec.EXACTLY;
} else if (consumeHorizontal) {
heightMode = MeasureSpec.EXACTLY;
}
//1.6 寬高大小绣硝,初始化為ViewPager可視區(qū)域中子View可用空間
int widthSize = childWidthSize;
int heightSize = childHeightSize;
//1.7 如果寬度不是wrap_content,那么width的測量模式就是EXACTLY
//如果寬度既不是wrap_content又不是match_parent撑刺,那么說明是用戶
//在布局文件寫的具體的尺寸鹉胖,直接將widthSize設(shè)置為這個具體尺寸
if (lp.width != LayoutParams.WRAP_CONTENT) {
widthMode = MeasureSpec.EXACTLY;
if (lp.width != LayoutParams.FILL_PARENT) {
widthSize = lp.width;
}
}
//1.8 同1.7
if (lp.height != LayoutParams.WRAP_CONTENT) {
heightMode = MeasureSpec.EXACTLY;
if (lp.height != LayoutParams.FILL_PARENT) {
heightSize = lp.height;
}
}
//1.9 合成Decor View的寬高specification(包含尺寸和模式的整數(shù))
final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, widthMode);
final int heightSpec = MeasureSpec.makeMeasureSpec(heightSize, heightMode);
//1.10 對DecorView進行測量
child.measure(widthSpec, heightSpec);
//1.11 如果Decor View占用了ViewPager的垂直方向的空間
//需要將子View的豎直方向可用的空間減去DecorView的高度,
//同理,水平方向上也做同樣的處理
if (consumeVertical) {
childHeightSize -= child.getMeasuredHeight();
} else if (consumeHorizontal) {
childWidthSize -= child.getMeasuredWidth();
}
}
}
}
//2.子View默認寬高的specification(包含尺寸和模式的整數(shù))
//(PS:mChildWidthMeasureSpec并沒有再次用到甫菠,個人感覺有點多余)
mChildWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);
mChildHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeightSize, MeasureSpec.EXACTLY);
//3.確保我們需要顯示的fragment已經(jīng)被我們創(chuàng)建好了
//populate()比較復(fù)雜败许,后面再詳細介紹
mInLayout = true;
populate();
mInLayout = false;
// 4.再對子View進行測量
size = getChildCount();
for (int i = 0; i < size; ++i) {
final View child = getChildAt(i);
//4.1 visibility為GONE的無需測量
if (child.getVisibility() != GONE) {
if (DEBUG) Log.v(TAG, "Measuring #" + i + " " + child
+ ": " + mChildWidthMeasureSpec);
//4.2 獲取子View的LayoutParams
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
//4.3 只針對子View而不對Decor View測量
if (lp == null || !lp.isDecor) {
//4.4 LayoutParams的widthFactor是取值為[0,1]的浮點數(shù),
// 用于表示子View占ViewPager顯示區(qū)域中子View可用寬度的比例淑蔚,
// 即(childWidthSize * lp.widthFactor)表示當前子View的實際寬度
final int widthSpec = MeasureSpec.makeMeasureSpec(
(int) (childWidthSize * lp.widthFactor), MeasureSpec.EXACTLY);
//4.5 對當前子View進行測量
child.measure(widthSpec, mChildHeightMeasureSpec);
}
}
}
}
onLayout
我們知道ViewPager的子View是水平擺放的市殷,所以在onLayout中,大部分工作的就是計算childLeft刹衫,即子View的左邊位置醋寝,而頂部位置基本上是一樣的,從以下代碼中可以體現(xiàn)的出來:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
//1.以下局部變量很簡單带迟,不再解釋
final int count = getChildCount();
int width = r - l;
int height = b - t;
int paddingLeft = getPaddingLeft();
int paddingTop = getPaddingTop();
int paddingRight = getPaddingRight();
int paddingBottom = getPaddingBottom();
final int scrollX = getScrollX();
//2.Decor View 數(shù)量
int decorCount = 0;
//3.首先對Decor View進行l(wèi)ayout音羞,再對普通子View進行l(wèi)ayout,
// 之所以先對Decor View布局仓犬,是為了讓普通子View能有合適的偏移
//下面循環(huán)主要是針對Decor View
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
//3.1 visibility不為GONE才layout
if (child.getVisibility() != GONE) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
//3.2 左邊和頂部的邊距初始化為0
int childLeft = 0;
int childTop = 0;
if (lp.isDecor) {//3.3 只針對Decor View
//3.4 獲取水平或垂直方向上的Gravity
final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
//3.5 根據(jù)水平方向上的Gravity嗅绰,確定childLeft以及paddingRight
switch (hgrav) {
default://沒有設(shè)置水平方向Gravity時(左中右),childLeft就取paddingLeft
childLeft = paddingLeft;
break;
case Gravity.LEFT://水平方向Gravity為left搀继,Decor View往最左邊靠
childLeft = paddingLeft;
paddingLeft += child.getMeasuredWidth();
break;
case Gravity.CENTER_HORIZONTAL://將Decor View居中擺放
childLeft = Math.max((width - child.getMeasuredWidth()) / 2,
paddingLeft);
break;
case Gravity.RIGHT://將Decor View往最右邊靠
childLeft = width - paddingRight - child.getMeasuredWidth();
paddingRight += child.getMeasuredWidth();
break;
}
//3.6 與3.5同理
switch (vgrav) {
default:
childTop = paddingTop;
break;
case Gravity.TOP:
childTop = paddingTop;
paddingTop += child.getMeasuredHeight();
break;
case Gravity.CENTER_VERTICAL:
childTop = Math.max((height - child.getMeasuredHeight()) / 2,
paddingTop);
break;
case Gravity.BOTTOM:
childTop = height - paddingBottom - child.getMeasuredHeight();
paddingBottom += child.getMeasuredHeight();
break;
}
//3.7 上面計算的childLeft是相對ViewPager的左邊計算的窘面,
//還需要加上x方向已經(jīng)滑動的距離scrollX
childLeft += scrollX;
//3.8 對Decor View布局
child.layout(childLeft, childTop,
childLeft + child.getMeasuredWidth(),
childTop + child.getMeasuredHeight());
//3.9 將Decor View數(shù)量+1
decorCount++;
}
}
}
//4.普通子View的寬度
final int childWidth = width - paddingLeft - paddingRight;
// Page views. Do this once we have the right padding offsets from above.
//5.下面針對普通子View布局,在此之前我們已經(jīng)得到正確的偏移量了
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
//5.1 ItemInfo 是ViewPager靜態(tài)內(nèi)部類叽躯,
// 它保存了普通子View的position财边、offset等信息,是對普通子View的一個抽象描述
ItemInfo ii;
//5.2 infoForChild通過傳入View查詢對應(yīng)的ItemInfo對象
if (!lp.isDecor && (ii = infoForChild(child)) != null) {
//計算當前子View的左邊偏移量
int loff = (int) (childWidth * ii.offset);
//將左邊距+左邊偏移量得到最終子View左邊位置
int childLeft = paddingLeft + loff;
int childTop = paddingTop;
//5.3 如果當前子View需要進行測量(measure)点骑,當這個子View是在Layout期間新添加新的酣难,
// 那么這個子View需要進行測量,即needsMeasure為true
if (lp.needsMeasure) {
//5.4 標記已經(jīng)測量過了
lp.needsMeasure = false;
//5.5 下面過程跟onMeasure類似
final int widthSpec = MeasureSpec.makeMeasureSpec(
(int) (childWidth * lp.widthFactor),
MeasureSpec.EXACTLY);
final int heightSpec = MeasureSpec.makeMeasureSpec(
(int) (height - paddingTop - paddingBottom),
MeasureSpec.EXACTLY);
child.measure(widthSpec, heightSpec);
}
if (DEBUG) Log.v(TAG, "Positioning #" + i + " " + child + " f=" + ii.object
+ ":" + childLeft + "," + childTop + " " + child.getMeasuredWidth()
+ "x" + child.getMeasuredHeight());
//5.6 對普通子View進行l(wèi)ayout
child.layout(childLeft, childTop,
childLeft + child.getMeasuredWidth(),
childTop + child.getMeasuredHeight());
}
}
}
//6. 將部分局部變量保存到實例變量中
mTopPageBounds = paddingTop;
mBottomPageBounds = height - paddingBottom;
mDecorChildCount = decorCount;
//7. 如果是第一次layout黑滴,則將ViewPager滑動到第一個子View的位置
if (mFirstLayout) {
scrollToItem(mCurItem, false, 0, false);
}
//8. 標記已經(jīng)布局過了憨募,即不再是第一次布局了
mFirstLayout = false;
}
好了,最基本的onMeasure和onLayout已經(jīng)分析完了袁辈,現(xiàn)在還不能滑動菜谣,接下來的文章將去分析一下ViewPager的滑動~