楼主分析的好透彻啊。重写ScrollView不如直接重写ViewGroup直接。 给你一个左右滑动的例子吧。 @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { if (!mIsDataReady) { return; }
if (DEBUG) Log.d(TAG, "PagedView.onLayout()"); //竖值方向的Padding final int verticalPadding = mPaddingTop + mPaddingBottom; final int childCount = getChildCount(); int childLeft = 0; if (childCount > 0) { if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", " + getChildWidth(0)); childLeft = getRelativeChildOffset(0); //偏移量为0 if (DEBUG) Log.d(TAG, "childLeft:"+childLeft);
// Calculate the variable page spacing if necessary // 如果mPageSpacing小于0的话,就重新计算mPageSpacing,并且给它赋值。 if (mPageSpacing < 0) { setPageSpacing(((right - left) - getChildAt(0).getMeasuredWidth()) / 2); } }
for (int i = 0; i < childCount; i++) { final View child = getPageAt(i); if (child.getVisibility() != View.GONE) { final int childWidth = getScaledMeasuredWidth(child); final int childHeight = child.getMeasuredHeight(); int childTop = mPaddingTop; if (mCenterPagesVertically) { childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2; }
给你一个左右滑动的例子吧。 @Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
if (!mIsDataReady) {
return;
}
if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
//竖值方向的Padding
final int verticalPadding = mPaddingTop + mPaddingBottom;
final int childCount = getChildCount();
int childLeft = 0;
if (childCount > 0) {
if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", "
+ getChildWidth(0));
childLeft = getRelativeChildOffset(0);
//偏移量为0
if (DEBUG) Log.d(TAG, "childLeft:"+childLeft);
// Calculate the variable page spacing if necessary
// 如果mPageSpacing小于0的话,就重新计算mPageSpacing,并且给它赋值。
if (mPageSpacing < 0) {
setPageSpacing(((right - left) - getChildAt(0).getMeasuredWidth()) / 2);
}
}
for (int i = 0; i < childCount; i++) {
final View child = getPageAt(i);
if (child.getVisibility() != View.GONE) {
final int childWidth = getScaledMeasuredWidth(child);
final int childHeight = child.getMeasuredHeight();
int childTop = mPaddingTop;
if (mCenterPagesVertically) {
childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2;
}
if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
//把5个CellLayout布局到相应的位置,layout的4个参数分别是 左、上、右、下。
child.layout(childLeft, childTop,
childLeft + child.getMeasuredWidth(), childTop + childHeight);
childLeft += childWidth + mPageSpacing;
}
}
//第一次布局完毕之后,就根据当前页偏移量(当前页距离Workspace最左边的距离)滚动到默认的页面去,第一次布局时
//默认的当前页是3,则它的便宜量就是两个CellLayout的宽度。
if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
setHorizontalScrollBarEnabled(false);
int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage);
//滚动到指定的位置
scrollTo(newX, 0);
mScroller.setFinalX(newX);
if (DEBUG) Log.d(TAG, "newX is "+newX);
setHorizontalScrollBarEnabled(true);
mFirstLayout = false;
}
if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) {
mFirstLayout = false;
}
}