自定义ViewGroup通常需要重写onMeasure()、onLayout()、onTouchEvent()方法。本文实现一个继承自ScrollView的自定义ViewGroup,监听手势添加一个惯性及回弹效果。
1、首先重写onMeasure()方法来测量ViewGroup的子View,遍历ViewGroup的子View,并调用measureChild()方法进行测量。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int count = getChildCount();
for (int i = 0; i < count; ++i) {
View childView = getChildAt(i);
measureChild(childView,
widthMeasureSpec, heightMeasureSpec);
}
}
2、测量完子View需要对其进行摆放,即设定子View在ViewGroup的位置。这里将子View设为全屏便于滑动操作,一般需要根据子View的属性WrapContent/FillParent来确定其布局位置。
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
// 设置ViewGroup的高度
MarginLayoutParams mlp = (MarginLayoutParams) getLayoutParams();
mlp.height = mScreenHeight * childCount;
setLayoutParams(mlp);
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
if (child.getVisibility() != View.GONE) {
child.layout(l, i * mScreenHeight,
r, (i + 1) * mScreenHeight);
}
}
}
3、监听手势,重写onTouchEvent()事件
@Overridepublic
boolean onTouchEvent(MotionEvent event) {
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: //手指放下,记录放下y坐标及View滑动y坐标
mLastY = y;
mStart = getScrollY();
break;
case MotionEvent.ACTION_MOVE: //手指移动,
if (!mScroller.isFinished()) { //滑动动画未结束,则终止动画
mScroller.abortAnimation();
}
int dy = mLastY - y;
//若已经到顶部且手指下滑,则不进行位移动画,即到顶不允许下拉动画
if (getScrollY() < 0) {
dy = 0;
}
//到底不允许上拉动画
if (getScrollY() > getHeight() - mScreenHeight) {
dy = 0;
}
scrollBy(0, dy);//View随手势滑动
mLastY = y;
break;
case MotionEvent.ACTION_UP://手指松开
mEnd = getScrollY();
int dScrollY = mEnd - mStart;
if (dScrollY > 0) {//手势向上滑动
if (dScrollY < mScreenHeight / 3) {//滑动距离未超过屏幕1/3,则回弹
mScroller.startScroll( 0, getScrollY(), 0, -dScrollY);
} else {//滑动超过1/3,继续惯性滑动剩余距离
mScroller.startScroll(0, getScrollY(), 0, mScreenHeight - dScrollY);
}
} else {//手势向下滑动
if (-dScrollY < mScreenHeight / 3) {
mScroller.startScroll(0, getScrollY(), 0, -dScrollY);
} else {
mScroller.startScroll( 0, getScrollY(),0, -mScreenHeight - dScrollY);
}
}
break;
}
postInvalidate();//更新UI,显示动画
return true;
}
4、重写computeScroll(),ViewGroup滑动到指定位置
@Override
public void computeScroll() {
super.computeScroll();
if (mScroller.computeScrollOffset()) {
scrollTo(0, mScroller.getCurrY());
postInvalidate();
}
}
5、初始化View
private void initView(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(
Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
mScreenHeight = dm.heightPixels;//获取当前手机屏幕高度
mScroller = new Scroller(context);//初始化Scroller辅助类
}
6、startScroll方法API
public void startScroll (int startX, int startY, int dx, int dy)
以提供的起始点和将要滑动的距离开始滚动,默认持续时间250ms。控制ViewGroup的内容进行滑动而非ViewGroup本身,且dx正值表示左滑,负值表示右滑。
参数:
startX 当前水平方向滑动偏移值,即当前ScrollY值,正值表明向左滑
startY 当前垂直方向滑动偏移值,即当前ScrollX值,正值表明向上滑
dx 水平方向滑动距离,正值表明向左滑动
dy 垂直方向滑动距离,正值表明向上滑动