自定义了一个Layout,无法正常显示

自定义了一个Layout,放在LinearLayout和Listview中时,顶部的能显示,不是顶部的无法显示,有大神知道原因么?
图片说明
贴下代码:

public class HorizontalSlideView extends LinearLayout{

private static final int TOUCH_MIN_DISTANCE = 5;
private View mSlideInLayout;
private View mDisplayLayout;
private Point mPoint=new Point();
private int mCurrentSlideDistance;
private int distance;
private Scroller scroller;
public HorizontalSlideView(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.HorizontalSlideView);
    distance= (int) ta.getDimension(R.styleable.HorizontalSlideView_horizontalSlideDistance, 60);
    int slideLayoutRes=ta.getResourceId(R.styleable.HorizontalSlideView_horizontalSlideInlayout, 0);
    mSlideInLayout= View.inflate(context, slideLayoutRes, null);

    int displayLayoutRes=ta.getResourceId(R.styleable.HorizontalSlideView_horizontalDisplaylayout, 0);
    mDisplayLayout= View.inflate(context, displayLayoutRes, null);
    ta.recycle();
    addView(mDisplayLayout);
    addView(mSlideInLayout);
    scroller=new Scroller(context,new DecelerateInterpolator());

}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    mDisplayLayout.measure(widthMeasureSpec, heightMeasureSpec);
    mSlideInLayout.measure(distance, heightMeasureSpec);

}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    // TODO 自动生成的方法存根
    super.onLayout(changed, l, t, r, b);
    mDisplayLayout.layout(l, t, r, b);
    mSlideInLayout.layout(r, t, r+distance, b);
}


@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    return super.dispatchTouchEvent(ev);
}

@Override
public void computeScroll() {
    super.computeScroll();
    if(!scroller.computeScrollOffset()){
        return;
    }
    int tempX=scroller.getCurrX();
    scrollTo(tempX, 0);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if(BuildConfig.DEBUG){
        Log.d(null, "in the onTouchEvent");
    }
    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        Log.i(null, "DOWN");
        mPoint=new Point((int)event.getX(), (int)event.getY());
        Log.i(null, "("+mPoint.x+","+mPoint.y+")");
        return true;
    case MotionEvent.ACTION_UP:
        Log.i(null, "UP");
        if(isHorizontalSlide(event))
            if(mPoint.x>event.getX()){
                int d=(int) (mCurrentSlideDistance+mPoint.x-event.getX()-TOUCH_MIN_DISTANCE);
                if(d<distance)
                    mCurrentSlideDistance+=(int) (mPoint.x-event.getX()-TOUCH_MIN_DISTANCE);
                else
                    mCurrentSlideDistance=distance;
            }else{
                mCurrentSlideDistance+=(int) (mPoint.x-event.getX()+TOUCH_MIN_DISTANCE);    

                int d=(int) (mCurrentSlideDistance+mPoint.x-event.getX()+TOUCH_MIN_DISTANCE);
                if(d>0)
                    mCurrentSlideDistance+=(int) (mPoint.x-event.getX()+TOUCH_MIN_DISTANCE);
                else
                    mCurrentSlideDistance=0;
            }
        if(mCurrentSlideDistance<distance/2){
            scroller.startScroll(mCurrentSlideDistance, 0, 0-mCurrentSlideDistance, 0);
            mCurrentSlideDistance=0;
        }else{
            scroller.startScroll(mCurrentSlideDistance, 0, distance-mCurrentSlideDistance, 0);
            mCurrentSlideDistance=distance;
        }
        invalidate();
        break;
    case MotionEvent.ACTION_MOVE:
        Log.i(null, "MOVE");
        if(isHorizontalSlide(event)){
            Log.i     (null, "SCROLL");
            //              System.out.println("mCurrentSlideDistance:"+mCurrentSlideDistance+",mPoint.x:"+mPoint.x+",event.getX:"+event.getX()+"TOUCH_MIN_DISTANCE:"+TOUCH_MIN_DISTANCE);
            if(mPoint.x>event.getX()){
                int d=(int) (mCurrentSlideDistance+mPoint.x-event.getX()-TOUCH_MIN_DISTANCE);
                if(d<distance)
                    scrollTo(d, 0);
                else
                    scrollTo(distance, 0);
            }
            else{
                int d=(int) (mCurrentSlideDistance+mPoint.x-event.getX()+TOUCH_MIN_DISTANCE);
                if(d>0)
                    scrollTo(d, 0);
                else
                    scrollTo(0, 0);
            }
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        Log.i(null, "CANCEL");
        break;
    default:
        Log.i(null, "ELSE");
        break;

    }
    return super.onTouchEvent(event);
}

private boolean isHorizontalSlide(MotionEvent event) {
    Point point=new Point((int)event.getX(), (int)event.getY());
    if(Math.abs(point.x-mPoint.x)>TOUCH_MIN_DISTANCE&&Math.abs(point.x-mPoint.x)>Math.abs(point.y-mPoint.y)){
        Log.i(null, "is slide");
        return true;
    }else{
        Log.i(null, "not slide");
        return false;
    }
}

}


查看全部
I__YaNG
I__YaNG
2015/08/13 11:03
  • android
  • 自定义控件
  • 点赞
  • 收藏
  • 回答
    私信

2个回复