Seekbar's thumb on click listner
I would like to register a clickable event on the seekbar's thumb in order to open activity when the user has cliked it. Is it possible? I have found similar question here Seekbar's thumb on click but found no answer. I don't want to move seek bar but want to open activity when user clicks only on the thump of seekbar.
This will fire when the user clicks on the thumb. See the beginning of onProgressChanged() Hope this helps. :)
If you have any questions about how it works feel free to comment.
mSeekBarSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progress = mSeekBarSpeed.getProgress();
boolean started = false; //use this variable to see whether the user clicked the right place
@Override
public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
if(!started){ //check to see if user clicks the right place
//if the user clicks within a specific threshold
float threshold = (float)seekBar.getMax() / seekBar.getWidth() * seekBar.getThumb().getIntrinsicWidth() / 2;
if(Math.abs(progressValue - progress) < threshold){
if(fromUser){ //checks if user actually clicked it
started = true;
//IF YOU WANT TO DO SOMETHING RIGHT WHERE THE USER CLICKS IT HERE IS THE PLACE
}
}else{
seekBar.setProgress(progress); //set to original progress
onStopTrackingTouch(seekBar); //not really necessary, keep or delete based on your needs
return; //get out of method
}
}
if(started) {
progress = progressValue; //update progress variable
System.out.println("onProgressChanged:" + progress + "/" + seekBar.getMax());
//DO WHAT YOU NEED TO DO WHEN PROGRESS IS CHANGING
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
System.out.println("onStartTracking:" + progress + "/" + seekBar.getMax());
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
System.out.println("onStopTracking:" + progress + "/" + seekBar.getMax());
//DO WHATEVER YOU NEED TO DO WHEN PROGRESS IS DONE CHANGING
started = false; //remember to set variable to false
}
});
链接地址: http://www.djcxy.com/p/64338.html
上一篇: 检测Android SeekBar的Thumb上的点击事件
下一篇: Seekbar的拇指点击列表器