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性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...