if (lp.isDecor) {
…
}
}
}
…
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
ItemInfo ii;
if (!lp.isDecor && (ii = infoForChild(child)) != null) {
int loff = (int) (childWidth * ii.offset);
int childLeft = paddingLeft + loff;
int childTop = paddingTop;
if (lp.needsMeasure) {
// This was added during layout and needs measurement.
// Do it now that we know what we’re working with.
lp.needsMeasure = false;
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());
}
child.layout(childLeft, childTop,
childLeft + child.getMeasuredWidth(),
childTop + child.getMeasuredHeight());
}
}
}
}
上面代码中,对 count
进行了两轮循环,其中第一轮是针对 lp.isDecor
为 true
的,
意为:如果当前view是一个 decoration 装饰,并不是adapter提供的view 则返回 true
显然,我们要探讨的是 adapter提供的View 是如何摆放的,所以忽略这一块。
而在下面的循环中,可以看到
child.layout(childLeft, childTop,
childLeft + child.getMeasuredWidth(),
childTop + child.getMeasuredHeight());
这个便是child的排布的核心代码,追溯这4个参数,可以得知:第 1,3 参数 表示 left ,right , 他们都和一个 int loff = (int) (childWidth * ii.offset);
挂钩,而 第2,4 参数表示 top,bottom , 则 并没有与 任何动态参数相挂钩。
因此可以断定,ViewPager
的子View
排布,只会存在X轴方向上的位置偏差,