Android控件重写

Gongqingshuai 2012-05-16 05:30:12
问一下,哪位重写过ScrolView控件,麻烦给个例子贴一下,重写的越彻底越好,谢谢
...全文
447 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sandys123sss 2012-06-13
  • 打赏
  • 举报
回复
无用代码。。‘
manoel 2012-05-17
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
问一下,哪位重写过ScrolView控件,麻烦给个例子贴一下,重写的越彻底越好,谢谢
[/Quote]
楼主分析的好透彻啊。重写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;
}

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;
}
}

Gongqingshuai 2012-05-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
我重写过,在里面加了提示上下可滑动的头
[/Quote]
可以贴下代码吗
himi_ 2012-05-17
  • 打赏
  • 举报
回复
我重写过,在里面加了提示上下可滑动的头
Gongqingshuai 2012-05-17
  • 打赏
  • 举报
回复
我在想是不是以后发帖,前面都加一句 “一个女孩的困惑。。。”,或者“一个女生的不解”,之类的话,问题会好问很多呢??

80,493

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧