Seekbar's thumb on click
I would like to register a clickable event on the seekbar's thumb in order to trigger an event when the user has cliked it. Is it possible?
Combining zwebie and Nermeens answers to the proper solution:
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
private int mProgressAtStartTracking;
private final int SENSITIVITY;
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
// handle progress change
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
mProgressAtStartTracking = seekBar.getProgress();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if(Math.abs(mProgressAtStartTracking - seekBar.getProgress()) <= SENSITIVITY){
// react to thumb click
}
}
});
So this really fires only on thumb click, not on thumb move and not on click on other parts of the seekbar.
You can adjust sensitivity, because sometimes a click already moves the thumb a little bit and allowing little changes while still clicking is less frustrating. A good value here depends on the size of the seekbar and the max value it can have. For me 3 worked well on a full-width seekbar with 50 max on a portrait layout.
I was able to achieve this in the following way:
seekBar.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_MOVE)
{
changedPosition = true;
seekBar.setProgress(seekBar.getProgress());
return false;
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
if(!changedPosition)
{
//do action here
}
}
}
hope this helps
我这样做:
pagesSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { //listener
@Override
public void onStopTrackingTouch(final SeekBar seekBar) {
//add your event here
}
@Override
public void onStartTrackingTouch(final SeekBar seekBar) {
}
@Override
public void onProgressChanged(final SeekBar seekBar, final int progress, final boolean fromUser) {
updateControls(progress, false);
}
});
链接地址: http://www.djcxy.com/p/11040.html
上一篇: 三星电视视频缓存使用HTML5和JS
下一篇: 单击Seekbar的拇指