2017-02-08 113 views
4

我有一個垂直搜索欄在我的Android項目。垂直搜索欄被初始化在onCreate方法:爲什麼OnSeekBarChangeListener總是返回布爾變量false?

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); 
} 
} 

回答

2

我沒有得到的答案就是爲什麼它的返回值false。但我得到了替代解決方案來知道天氣用戶更改了seekbar值,或者您改變了seekbar值編碼。我正在使用取決於微調器值的方式更改seekbar值。因此,採取可變

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個元素一樣。

那麼,這僅僅是一個替代的解決方案。如果你面臨同樣的問題,你可以使用它。