跟着徐宜生学Android——自定义ViewGroup

2017-10-09  本文已影响0人  心中愁眼前秋

自定义ViewGroup的基本流程

重写两个方法和LayoutParams

onMeasure()
通过measureChildren()来通知每一个子View自己测量(遍历子View)。我们只需要考虑ViewGroup的宽高在自适应的情况下该是多大。

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    MarginLayoutParams params = null;//布局参数
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);//测量两元素Mode和Size
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    measureChildren(widthMeasureSpec,heightMeasureSpec);//遍历子View

    //开始处理wrap_content,如果一个子元素都没有,就设置为0
    if (getChildCount() == 0) {
        setMeasuredDimension(0,0);
    } else if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
        //ViewGroup,宽,高都是wrap_content,根据我们的需求,宽度是子控件的宽度,高度则是所有子控件的总和
        View childOne = getChildAt(0);
        params = (MarginLayoutParams) childOne.getLayoutParams();
        int childWidth = childOne.getMeasuredWidth();
        int childHeight = childOne.getMeasuredHeight();
        setMeasuredDimension(childWidth + params.leftMargin + params.rightMargin,
                childHeight * getChildCount() + params.topMargin + params.bottomMargin);

    } else if (widthMode == MeasureSpec.AT_MOST) {
        //ViewGroup的宽度为wrap_content,则高度不需要管,宽度还是自控件的宽度
        View childOne = getChildAt(0);
        params = (MarginLayoutParams) childOne.getLayoutParams();
        int childWidth = childOne.getMeasuredWidth();
        setMeasuredDimension(childWidth + params.leftMargin + params.rightMargin,heightSize);
    } else if (heightMode == MeasureSpec.AT_MOST) {
        //ViewGroup的高度为wrap_content,则宽度不需要管,高度为子View的高度和
        View childOne = getChildAt(0);
        params = (MarginLayoutParams) childOne.getLayoutParams();
        int childHeight = childOne.getMeasuredHeight();
        setMeasuredDimension(widthSize, childHeight * getChildCount() + params.topMargin + params.bottomMargin);
    }
}

onLayout()
摆放子View的位置

protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int height = 0;
    int count = getChildCount();
    View child;
    Log.e("ri", count + "");
    child = getChildAt(0);
    MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
    int c1 = params.leftMargin;
    int c2 = params.topMargin;
    int c3 = c1 + child.getMeasuredWidth();
    int c4 = c2 + child.getMeasuredHeight();
    child.layout(c1,c2, c3,c4);
    height = c4 + params.bottomMargin;
    for(int i = 1 ;i < count;i++) {
        child = getChildAt(i);
        child.layout(c1, height, c3, height + child.getMeasuredHeight());
        height += child.getMeasuredHeight();
    }
}

params = (MarginLayoutParams) child.getLayoutParams();如果重写layoutParams相关的代码,这样直接转换会出现问题,所以需要重写下面的代码:返回MarginLayoutParams类型的对象。

@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
    return new MarginLayoutParams(getContext(),attrs);
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
    return new MarginLayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT);
}
@Override
protected LayoutParams generateLayoutParams(LayoutParams p) {
    return new MarginLayoutParams(p);
}

onTouchEvent 响应时间

上一篇下一篇

猜你喜欢

热点阅读