【问题标题】:Can't update vertical seekbar programmatically (Without touching it)无法以编程方式更新垂直搜索栏(无需触摸)
【发布时间】:2016-09-05 22:33:57
【问题描述】:

我很长时间以来一直被这个问题所困扰。我离开了它然后又回来了,但到目前为止我还没有找到一个可行的解决方案。问题很简单,我有一个垂直搜索栏,我想以编程方式更新它而不触及它,但它没有发生。这是我的垂直搜索栏代码

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

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

这是在 xml 中声明搜索栏的方式

<com.gnr.kumar.varun.songapp.views.VerticalSeekBar
                android:id="@+id/slider_1"
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:max="100"
                android:padding="10dp"
                android:progress="50"
                android:progressBackgroundTint="#9cf2ec"
                android:progressTint="#9cf2ec"
                android:thumb="@drawable/thumb" />

我尝试做类似的事情

SeekBar sliders[] = new SeekBar[NUM_OF_SLIDERS];

sliders[0] = (SeekBar) findViewById(R.id.slider_1);

sliders[0].setProgress(100);

但它只是将搜索栏位置更改为略大于 0,而实际上它应该处于最大值。无论我用多大的数字代替 100,它都是一样的。有人可以帮忙吗!提前谢谢!!!

【问题讨论】:

  • 尝试旋转画布,我认为在 onSizeChanged/onMeasure 调用中反转尺寸可能是问题所在。
  • @zgc7009 谢谢输入,今天终于解决了。
  • 太棒了!如果您将来可以为其他人发布您的答案,我们将不胜感激
  • @zgc7009 是的,伙计,刚刚发布,从关于垂直搜索栏的问题的评论中得到了答案。只是在谷歌搜索后偶然发现了它。我认为它必须在stackoverflow上,因为这是一个非常普遍的事情。它被埋在 cmets 中:D

标签: android android-seekbar android-vertical-seekbar


【解决方案1】:

经过很长时间,我终于能够解决我的问题。我所要做的就是在 Vertical Seekbar 类中覆盖它。对this 问题的已接受答案的评论帮助了我。

@Override public synchronized void setProgress(int progress){           
    super.setProgress(progress);
    onSizeChanged(getWidth(), getHeight(), 0, 0); 
}

【讨论】:

    猜你喜欢
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    • 2015-08-19
    • 1970-01-01
    相关资源
    最近更新 更多