Ahmet K
Ahmet K

Reputation: 813

Detect Tap or Drag of a Seekbar in SeekbarChangeListener

Is there a way to decide if the user is tapping or is dragging on the Seekbar ?Because both of this actions are triggering the onStartTrackingTouch and onStopTrackingTouch event in the SeekbarChangeListener and for me it is important that it isn't a tap.

Thank you for your help !

Upvotes: 1

Views: 2403

Answers (2)

Jeffery Ma
Jeffery Ma

Reputation: 3351

here are two ways to detect the event

(1) Customize a Seekbar get the demo code in My another Answer

(2) according to Ahmet Kazaman method

  1. create an interface
  2. detector implement the interface
  3. invoke detector's method in SeekBar.OnSeekOnSeekBarChangeListener

mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            mDetector.onProgressChanged(fromUser);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            mDetector.onStartTrackingTouch();
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
           mDetector.onStopTrackingTouch();
           int type = mDetector.getActionType();
           if (type == IOnSeekBarChangeListener.ACTION_TYPE_CLICK) {
               //do click event
           } else if (type == IOnSeekBarChangeListener.ACTION_TYPE_DRAG) {
               //do drag event
           }
           mDetector.reset();
        }
    });

public interface IOnSeekBarChangeListener {


int ACTION_TYPE_DRAG = 0x10;
int ACTION_TYPE_CLICK = 0x20;

void onProgressChanged(boolean fromUser);

void onStartTrackingTouch();

void onStopTrackingTouch();

}

public class Detector implements IOnSeekBarChangeListener {

private static final int DRAG_THRESH_HOLDER = 3;
private boolean mStartTracking = false;
private int mOnChangedTimes = 0;
private int mActionType = 0;

@Override
public void onProgressChanged(boolean fromUser) {
    if (fromUser) {
        mOnChangedTimes++;
    }
}

@Override
public void onStartTrackingTouch() {
   mStartTracking = true;
}

@Override
public void onStopTrackingTouch() {
    if (mStartTracking) {
        if (mOnChangedTimes > DRAG_THRESH_HOLDER) {
            //this is a drag
            mActionType = ACTION_TYPE_DRAG;
        } else {
            //this is a click
            mActionType = ACTION_TYPE_CLICK;
        }
    }
    mStartTracking = false;
}

public int getActionType() {
    return mActionType;
}

public void reset() {
    mActionType = 0;
    mOnChangedTimes = 0;
}

}

Upvotes: 2

Ahmet K
Ahmet K

Reputation: 813

I found a solution on my own...

Firstly I logged the 3 Listener Events and I noticed that when a single tap happens that the sequence is everytime the same. The order is "onStartTrackingTouch","onProgressChanged" and finally "onStopTrackingTouch". The difference to dragging the Seekbar is that onProgressChanged is called more than once. So you come to the conclusion that a tap happens when onProgressChanged is called once. So a normal counter variable is enough to check which action happened. Hope this helps someone :)

Upvotes: 3

Related Questions