防止垂直搜索条在攻丝时移动

时间:2014-03-11 08:36:15

标签: android seekbar

我在水平列表视图中使用垂直搜索栏。现在搜索栏甚至在progressdrawable中的手指触摸时移动。但我只想在拇指移动时移动它。触摸progressdrawable时,不应更改搜索条值。我该如何预防?

public class VerticalSeekBar extends SeekBar {

public VerticalSeekBar(Context context) {
    super(context);
}

public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public VerticalSeekBar(Context context, AttributeSet attrs) {
    super(context, attrs);
}

protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(h, w, oldh, oldw);
}

@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(heightMeasureSpec, widthMeasureSpec);
    setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}

protected void onDraw(Canvas c) {
    c.rotate(-90);
    c.translate(-getHeight(),0);

    super.onDraw(c);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isEnabled()) {
        return false;
    }

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_MOVE:
            int i=0;
            i=getMax() - (int) (getMax() * event.getY() / getHeight());
            setProgress(i);
            onSizeChanged(getWidth(), getHeight(), 0, 0);
            break;

        case MotionEvent.ACTION_CANCEL:
            break;
    }
    return true;
}

Drawable mThumb;

@Override
public void setThumb(Drawable thumb) {
    super.setThumb(thumb);
    mThumb = thumb;
}
public Drawable getSeekBarThumb() {
    return mThumb;
}

public void updateThumb(){
    onSizeChanged(getWidth(), getHeight(), 0, 0);

} }

1 个答案:

答案 0 :(得分:2)

我解决了以下问题。如果在拇指图像外触摸,它不会改变搜索条值。

holder.vertical_seekBar.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    if(event.getAction() == MotionEvent.ACTION_MOVE ||
                            event.getAction() == MotionEvent.ACTION_UP ||
                            event.getAction() == MotionEvent.ACTION_DOWN){
                        Rect seekBarThumbRect = holder.vertical_seekBar.getSeekBarThumb().getBounds();
                        int seekBarHeight = holder.vertical_seekBar.getHeight();
                        if(seekBarThumbRect.left - (seekBarThumbRect.right - seekBarThumbRect.left) / 2 < (Math.abs(seekBarHeight - event.getY())) &&
                                seekBarThumbRect.right + (seekBarThumbRect.right - seekBarThumbRect.left) / 2 > (Math.abs(seekBarHeight - event.getY())) &&
                                seekBarThumbRect.top < event.getX() &&
                                seekBarThumbRect.bottom > event.getX())
                                        return false;
                    }
                    return true;
                }
            });