Frage

I'm using vertical seek bar in horizontal list view.Right now the seekbar moves even on finger touch in the progressdrawable. But I want to move it only when the thumb is moved. When touch the progressdrawable the seekbar value should not be change. How can i prevent it?

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);

} }

War es hilfreich?

Lösung

I solved the problem as follows. It does not change seek bar value if touched outside the thumb image.

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;
                }
            });
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top