为什么OnSeekBarChangeListener总是返回布尔变量false?

时间:2017-02-08 09:28:01

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

我的android项目中有一个Vertical Seekbar。 Vertical on Seekbar在onCreate方法中初始化:

verticalSeekBar1 = (VerticalSeekBar) findViewById(R.id.verticalSeekbar1);
verticalSeekBar1.setOnSeekBarChangeListener(Listener1);

我必须实现OnSeekBarChangeListener接口,如下所示

private SeekBar.OnSeekBarChangeListener Listener1 = new SeekBar.OnSeekBarChangeListener() {

@Override                                                                                       
public void onProgressChanged(SeekBar arg0, int seekBarLevel, boolean changedByUser) {          

    verticalSeekBar1.setProgress(seekBarLevel);                                                 

    Log.e(TAG, "onProgressChanged: UserChnagedSeekbar :: "+changedByUser);                                                                         

    if (changedBySpinner){                                                                     
        mSpinner.setSelection(0);                                                
    }                                                                 
}

@Override                                       
public void onStartTrackingTouch(SeekBar arg0) {
    // TODO Auto-generated method stub          

}                                               

@Override                                       
public void onStopTrackingTouch(SeekBar arg0) { 
    // TODO Auto-generated method stub          

}                                               
};

changedByUser始终返回false。我是否改变了Seekbar。请帮我解决这个问题。提前谢谢。

我的自定义垂直搜索栏类代码

public class VerticalSeekBar extends SeekBar {

private Context mContext;

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

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

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

private void init(Context context){
    mContext=context;
}

protected void onSizeChanged(int w, int h, int oldWidth, int oldHeight) {
    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) {

    Paint paint=new Paint();
    paint.setColor(Color.DKGRAY);
    paint.setStrokeWidth(2.0f);
    Rect rect=new Rect(0,0,getMeasuredWidth(),getMeasuredHeight());
    //c.drawRect(rect,paint);

    int vWidth=getMeasuredWidth();
    int vHeight=getMeasuredHeight();

    float startPointY=83;
    float middlePointY = (vHeight)/2;
    float lastPointY=vHeight-83;

    paint.setColor(Color.DKGRAY);
    c.drawLine(rect.left,startPointY,rect.centerX(),startPointY,paint);
    c.drawLine(rect.left,middlePointY,rect.centerX(),middlePointY,paint);
    c.drawLine(rect.left,lastPointY,rect.centerX(),lastPointY,paint);

    paint.setColor(Color.DKGRAY);

    float x=((vHeight/2)-startPointY)/3;
    float f10dbPoint=x+startPointY;
    float f5dbPoint=(2*x)+startPointY;
    float fm5dbPoint=(vHeight/2)+x;
    float fm10dbPoint=(vHeight/2)+(2*x);

    float startX=rect.left+32;
    c.drawLine(startX,f10dbPoint,rect.centerX(),f10dbPoint,paint);
    c.drawLine(startX,f5dbPoint,rect.centerX(),f5dbPoint,paint);
    c.drawLine(startX,fm5dbPoint,rect.centerX(),fm5dbPoint,paint);
    c.drawLine(startX,fm10dbPoint,rect.centerX(),fm10dbPoint,paint);

    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:
            int i=0;
            i=getMax() - (int) (getMax() * event.getY() / getHeight());
            setProgress(i);
            Log.i("Progress",getProgress()+"");
            onSizeChanged(getWidth(), getHeight(), 0, 0);
            break;

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

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

1 个答案:

答案 0 :(得分:2)

我没有得到答案,为什么它的返回值为假。但是我得到了替代解决方案,以了解天气用户改变的搜索栏值或者您已经改变了搜索栏值编码。我根据微调器值改变了搜索栏值。所以采取变量

boolean changedBySpinner=false;

内部微调器ItemClickListener使其成为真实

mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            changedBySpinner=true;
   }
});

在搜索栏上设置touchClickListener(垂直搜索栏)

verticalSeekBar1.setOnTouchListener((View.OnTouchListener) this);

在onTouchMethod内部使该变量为真

changedBySpinner=false;

在seekbarChangeListener里面添加以下代码

 private SeekBar.OnSeekBarChangeListener Listener1 = new SeekBar.OnSeekBarChangeListener() {

    @Override
    public void onProgressChanged(SeekBar arg0, int seekBarLevel, boolean changedByUser) {

        if (!changedBySpinner){
            mSpinner.setSelection(0);
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub

    }

};

执行您想要的操作,如果阻止内部。就像我将微调器值设置为第0个元素。

嗯,这只是一个替代解决方案。如果您遇到同样的问题,可以使用它。