其他分享
首页 > 其他分享> > android – 为什么OnSeekBarChangeListener总是返回布尔变量false?

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

作者:互联网

我的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.请帮我解决这个问题.先感谢您.

我的自定义垂直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);
}
}

解决方法:

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

boolean changedBySpinner=false;

在Spinner内部,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内部使该变量为true

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个元素.

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

标签:android,android-seekbar,android-vertical-seekbar
来源: https://codeday.me/bug/20190701/1351158.html