android 继承ViewGroup实现自定义布局

private Scroller mScroller;

private VelocityTracker mVelocityTracker;

public HorizontalScrollViewEx(Context context) {

super(context);

init();

}

public HorizontalScrollViewEx(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

public HorizontalScrollViewEx(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

init();

}

private void init() {

if (mScroller==null){

mScroller = new Scroller(getContext());

mVelocityTracker = VelocityTracker.obtain();

}

}

@Override

public boolean onInterceptTouchEvent(MotionEvent ev) {

boolean intercepted = false;

int x = (int)ev.getX();

int y = (int)ev.getY();

switch (ev.getAction()){

case MotionEvent.ACTION_DOWN:

intercepted = false;

if (!mScroller.isFinished()){

mScroller.abortAnimation();

intercepted = true;

}

break;

case MotionEvent.ACTION_MOVE:

int detlaX = x-mLastXIntercept;

int detlaY = y-mLastYIntercept;

if (Math.abs(detlaX) > Math.abs(detlaY)){

intercepted = true;

}else{

intercepted = false;

}

break;

case MotionEvent.ACTION_UP:

intercepted = false;

break;

default:

break;

}

Log.d(TAG,"intercepted = "+intercepted);

mLastX = x;

mLastY = y;

mLastXIntercept = x;

mLastYIntercept = y;

return intercepted;

}

@Override

public boolean onTouchEvent(MotionEvent event) {

mVelocityTracker.addMovement(event);

int x = (int)getX();

int y = (int)getY();

switch (event.getAction()){

case MotionEvent.ACTION_DOWN:

if (!mScroller.isFinished()){

mScroller.abortAnimation();

}

break;

case MotionEvent.ACTION_MOVE:

int detalX = x-mLastX;

int detalY = y-mLastY;

scrollBy(-detalX,0);

break;

case MotionEvent.ACTION_UP:

int scrollX = getScrollX();

mVelocityTracker.computeCurrentVelocity(1000);

float xVelocity = mVelocityTracker.getXVelocity();

if (Math.abs(xVelocity) >= 50){

mChildIndex = xVelocity > 0 ? mChildIndex -1 : mChildIndex + 1;

}else{

mChildIndex = (scrollX + mChildWidth / 2) / mChildWidth;

}

mChildIndex = Math.max(0,Math.min(mChildIndex,mChildrenSize -1 ));

int dx = mChildIndex * mChildWidth - scrollX;

smoothScrollBy(dx, 0);

mVelocityTracker.clear();

break;

default:

break;

}

mLastX = x;

mLastY = y;

return true;

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int measuredWidth = 0;

int measuredHeight = 0;

//拿到自己含有子元素的个数

final int childCount = getChildCount();

measureChildren(widthMeasureSpec,heightMeasureSpec);

int widthSpaceSize = MeasureSpec.getSize(widthMeasureSpec);

int widthSpaceMode = MeasureSpec.getMode(widthMeasureSpec);

int heightSpaceSize = MeasureSpec.getSize(heightMeasureSpec);

int heightSpaceMode = MeasureSpec.getMode(heightMeasureSpec);

/**

  • 1.判断是否含有子元素,如果没有直接把自己的高度和宽度设置为0

  • 2.判断子元素的宽度和高度是否采用的warp_content,如果采用了则把所有的子元素的宽度之和给自己并且把

  • 第一个子元素的高度给自己。

  • 3.再进行分别判断高度和宽度那个用了warp_content,就对其分别设置不一样的值。

*/

if (childCount == 0){

setMeasuredDimension(0,0);

}else if (widthSpaceMode == MeasureSpec.AT_MOST && heightSpaceMode == MeasureSpec.AT_MOST){

final View childView = getChildAt(0);

measuredWidth = childView.getMeasuredWidth() * childCount;

measuredHeight = childView.getMeasuredHeight();

setMeasuredDimension(measuredWidth,measuredHeight);

}else if (heightSpaceMode == MeasureSpec.AT_MOST){

final View childView = getChildAt(0);

measuredHeight = childView.getMeasuredHeight();

setMeasuredDimension(widthSpaceSize,childView.getMeasuredHeight());

}else if (widthSpaceMode == MeasureSpec.AT_MOST){

final View childView = getChildAt(0);

measuredWidth = childView.getMeasuredWidth() * childCount;

setMeasuredDimension(measuredWidth,heightSpaceSize);

}

/**

  • 以上代码有两个不规范的地方:

  • 1.如果没有子元素的时候,不应该直接把宽度/高度直接设置为0,而是要根据LayoutParams中的高度/宽度来做相应的处理

  • 2.在测量的时候要考虑到自己和子元素的padding和margin值,这样测出来的才是最标准的

*/

}

/**

  • 完成子元素的定位:

  • 1.遍历所有的子元素是否处于GONE的状态,不是则放置在合适的位置

  • 放置的过程也是由左向右

  • 缺点:也是没有考虑到自己个子元素的padding和maigin的值

  • @param changed

  • @par
    am l

  • @param t

  • @param r

  • @param b

*/

@Override

  • 16
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值