我有一个seekbar滑动条,我希望只允许使用拇指/手柄部分来更改它。如果用户在其他任何地方点击,我希望忽略该点击。
是否有固有属性可实现此功能?如果没有,我已经知道可以设置onTouch监听器并返回true
来“禁用”它,但是否有一种方法可以检测到拇指被点击,以便我可以告诉onTouch在这种情况下返回false
?
我有一个seekbar滑动条,我希望只允许使用拇指/手柄部分来更改它。如果用户在其他任何地方点击,我希望忽略该点击。
是否有固有属性可实现此功能?如果没有,我已经知道可以设置onTouch监听器并返回true
来“禁用”它,但是否有一种方法可以检测到拇指被点击,以便我可以告诉onTouch在这种情况下返回false
?
我猜你想通过代码来改变缩略图位置,但这是受用户限制的。所以尝试使用以下方法,可能有帮助。
myseekBar.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {
int originalProgress;
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
originalProgress = seekBar.getProgress();
}
@Override
public void onProgressChanged(SeekBar seekBar, int arg1, boolean fromUser) {
if( fromUser == true){
seekBar.setProgress(originalProgress);
}
}
});
// I solved this problem.
private int mOriginProgress;
private boolean mIsDrag;
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
mIsTouched = true;
mOriginProgress = seekBar.getProgress();
}
@Override
public void onProgressChanged(SeekBar seekBar, int p, boolean fromUser) {
if (mOriginProgress == p) {
return;
}
if (mIsDrag) {
// do something
} else {
if (Math.abs(mOriginProgress - p) > 5) {
seekBar.setProgress(mOriginProgress);
} else {
mIsDrag = true;
}
}
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mIsDrag = false;
}
我曾经遇到过同样的问题,我使用了一个简单的逻辑来比较新进度和旧进度,最终解决了这个问题。以下是我的代码,用于实现一个垂直滑块,只有在用户点击拇指时才允许更改值。
这就是诀窍:
int tempProgress = (getMax() - (int) (getMax() * event.getY() / getHeight()));
int diff = Math.abs(tempProgress - progress);
if ( diff > 7 && !isMoving) return true;
isMoving = true;
这是所有组件的源代码:
package android.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class VerticalSeekBar extends SeekBar {
private OnSeekBarChangeListener onChangeListener;
private int progress = 0;
private Drawable mThumb;
private boolean isMoving;
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 void setOnSeekBarChangeListener(OnSeekBarChangeListener onChangeListener){
this.onChangeListener = onChangeListener;
}
public void setThumb(Drawable thumb) {
if (thumb != null) {
thumb.setCallback(this);
}
super.setThumb(thumb);
mThumb = thumb;
mThumb.setCallback(this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if ( onChangeListener!= null)onChangeListener.onStartTrackingTouch(this);
setPressed(true);
setSelected(true);
break;
case MotionEvent.ACTION_MOVE:
int tempProgress = (getMax() - (int) (getMax() * event.getY() / getHeight()));
int diff = Math.abs(tempProgress - progress);
if ( diff > 7 && !isMoving) return true;
isMoving = true;
progress = getMax() - (int) (getMax() * event.getY() / getHeight());
if(progress < 18) {progress = 18;}
if(progress > getMax()) {progress = getMax();}
setProgressAndThumb(progress);
setPressed(true);
setSelected(true);
break;
case MotionEvent.ACTION_UP:
isMoving = false;
if ( onChangeListener!= null)onChangeListener.onStopTrackingTouch(this);
setPressed(false);
setSelected(false);
break;
case MotionEvent.ACTION_CANCEL:
isMoving = false;
super.onTouchEvent(event);
setPressed(false);
setSelected(false);
break;
}
return true;
}
public synchronized void setProgressAndThumb(int progress) {
setProgress(progress);
onSizeChanged(getWidth(), getHeight() , 0, 0);
if ( onChangeListener!= null) onChangeListener.onProgressChanged(this, progress, true);
}
@Override
public synchronized void setProgress(int progress) {
this.progress =progress;
super.setProgress(progress);
if ( onChangeListener !=null) onChangeListener.onProgressChanged(this, progress, true);
}
public synchronized void setMaximum(int maximum) {
setMax(maximum);
}
public synchronized int getMaximum() {
return getMax();
}
}
祝好!