1

这是我的 VerticalSeeker 课程:

public class VerticalSeekBar extends SeekBar implements SeekBar.OnSeekBarChangeListener{

    public VerticalSeekBar(Context context) {
        super(context);
        this.setOnSeekBarChangeListener(this);
    }

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

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

    public 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_MOVE:
        case MotionEvent.ACTION_UP:
            setProgress((int) (getMax() * event.getY() / getHeight()) - 0);
            onSizeChanged(getWidth(), getHeight(), 0, 0);
            break;

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

@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}

@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
    Toast.makeText(this.getContext(), "Start", Toast.LENGTH_SHORT).show();
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    Toast.makeText(this.getContext(), "Stop", Toast.LENGTH_SHORT).show();
}

}

有人可以解释一下,为什么只有“onProgressChanged”事件正在触发?如何让“onStartTrackingTouch”和“onStopTrackingTouch”工作?

4

2 回答 2

4

至少将您的 Motion 事件切换案例代码更改为

case MotionEvent.ACTION_UP:
  setProgress(getMax()- (int) (getMax() * event.getY() / getHeight()));
  onSizeChanged(getWidth(), getHeight(), 0, 0); 
  onChangeListener.onStopTrackingTouch(this);

在http://hackskrieg.wordpress.com/2012/04/20/working-vertical-seekbar-for-android/找到了完整的实现

于 2013-02-02T06:48:53.457 回答
0

公共类 VerticalSeekBar 扩展 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);
}

private OnSeekBarChangeListener onChangeListener;
@Override
public void setOnSeekBarChangeListener(OnSeekBarChangeListener onChangeListener){
    this.onChangeListener = onChangeListener;
}

private int lastProgress = 0;
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (!isEnabled()) {
        return false;
    }

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        onChangeListener.onStartTrackingTouch(this);
        setPressed(true);
        setSelected(true);
        break;
    case MotionEvent.ACTION_MOVE:
        super.onTouchEvent(event);
        int progress = getMax() - (int) (getMax() * event.getY() / getHeight());

        // Ensure progress stays within boundaries
        if(progress < 0) {progress = 0;}
        if(progress > getMax()) {progress = getMax();}
        setProgress(progress);  // Draw progress
        if(progress != lastProgress) {
            // Only enact listener if the progress has actually changed
            lastProgress = progress;
            onChangeListener.onProgressChanged(this, progress, true);
        }

        onSizeChanged(getWidth(), getHeight() , 0, 0);
        setPressed(true);
        setSelected(true);
        break;
    case MotionEvent.ACTION_UP:
        onChangeListener.onStopTrackingTouch(this);
        setPressed(false);
        setSelected(false);
        break;
    case MotionEvent.ACTION_CANCEL:
        super.onTouchEvent(event);
        setPressed(false);
        setSelected(false);
        break;
    }
    return true;
}

public synchronized void setProgressAndThumb(int progress) {
    setProgress(progress);
    onSizeChanged(getWidth(), getHeight() , 0, 0);
    if(progress != lastProgress) {
        // Only enact listener if the progress has actually changed
        lastProgress = progress;
        onChangeListener.onProgressChanged(this, progress, true);
    }
}

public synchronized void setMaximum(int maximum) {
    setMax(maximum);
}

public synchronized int getMaximum() {
    return getMax();
}

}

于 2015-09-24T07:55:02.830 回答