检测seekbar thumb的长按事件

时间:2016-09-17 21:04:20

标签: android views

我的Android应用程序中有一个搜索栏。我需要检测搜索栏的长按事件以执行某些操作。建议我任何解决方案。我在搜索栏上尝试了onLongClickListener,但它无效。

2 个答案:

答案 0 :(得分:1)

在阅读开发人员的文档和其他SeekBar LongClick问题后,LongClickListener似乎无法使用SeekBar

但是,我可以建议一个解决方法:

  1. SeekBar:您可以为搜索栏实施onTouch事件,对于MotionEvent.ACTION_DOWN操作,您可以设置超时1秒,并将其视为LongClick。如果调用MotionEvent.ACTION_UP,则取消计时器。

  2. Thumb:您可以实施OnSeekBarChangeListener,在onStartTrackingTouch事件中,您可以将暂停时间设置为1秒,并将LongClick视为onStopTrackingTouch或{{onProgressChanged超时期间不调用1}}(如果调用onStopTrackingTouch / onProgressChanged,则取消定时器。)

  3. 未发布代码,因为我的IDE现在尚未打开。但我希望你能找到使用这种逻辑的方法。 (如果您不了解我提到的事件,请阅读文件)

    谢谢。

答案 1 :(得分:0)

定制的SeekBar,处理onTouchEvent()

中的逻辑

LongClick

ACTION_DOWN postDelay()长按可运行时,您可以根据需要修改LONG_CLICK_DELAY的值。 ACTION_MOVE ACTION_UP ACTION_CANCEL删除可运行文件

点击

ACTION_UP检查ACTION_DOWN运动事件参数时,请确保它是点击事件


     mSeekBar.setEventListener(new DetectorSeekBar.IListener() {
        @Override
        public void onClick(DetectorSeekBar detectorSeekBar) {
            Log.e("test", "click");
        }

        @Override
        public void onLongClick(DetectorSeekBar detectorSeekBar) {
            Log.e("test", "long click");
        }
    });

public class DetectorSeekBar extends AppCompatSeekBar {

//modified the value as you wish
private static final int LONG_CLICK_DELAY = 500;

private LongClickChecker mLongClickChecker;
private ClickChecker mClickChecker;
private IListener mListener;

public DetectorSeekBar(Context context) {
    this(context, null);
}

public DetectorSeekBar(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public DetectorSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mLongClickChecker = new LongClickChecker(this);
    mClickChecker = new ClickChecker(this);
}

public void setEventListener(IListener listener) {
    mListener = listener;
    mLongClickChecker.setLongClickListener(listener);
    mClickChecker.setClickListener(listener);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (isEnabled()) {
                postDelayed(mLongClickChecker, LONG_CLICK_DELAY);
                mClickChecker.x = event.getX();
                mClickChecker.y = event.getY();
                mClickChecker.time = event.getEventTime();
            }
            break;
        case MotionEvent.ACTION_MOVE:
            removeCallbacks(mLongClickChecker);
            mClickChecker.onMoveEvent();
            break;
        case MotionEvent.ACTION_UP:
            removeCallbacks(mLongClickChecker);
            if (isEnabled()
                    && mClickChecker.checkCondition(event)) {
                post(mClickChecker);
            }
            break;
        case MotionEvent.ACTION_CANCEL:
            removeCallbacks(mLongClickChecker);
            removeCallbacks(mClickChecker);
            break;
    }
    return super.onTouchEvent(event);
}

private static class LongClickChecker implements Runnable {

    private WeakReference<IListener> mListenerRef;
    private WeakReference<DetectorSeekBar> mViewRef;

    LongClickChecker(DetectorSeekBar view) {
        mViewRef = new WeakReference<>(view);
    }

    void setLongClickListener(IListener listener) {
        mListenerRef = new WeakReference<>(listener);
    }

    @Override
    public void run() {
        if (mListenerRef != null && mListenerRef.get() != null
                && mViewRef != null && mViewRef.get() != null) {
            mListenerRef.get().onLongClick(mViewRef.get());
        }
    }
}

private static class ClickChecker implements Runnable {

    private long time = 0;
    public float x;
    public float y;
    private boolean mMoved = false;

    private WeakReference<IListener> mListenerRef;
    private WeakReference<DetectorSeekBar> mViewRef;

    ClickChecker(DetectorSeekBar view) {
        mViewRef = new WeakReference<>(view);
    }

    @Override
    public void run() {
        if (mListenerRef != null && mListenerRef.get() != null
                && mViewRef != null && mViewRef.get() != null) {
            mListenerRef.get().onClick(mViewRef.get());
        }
    }

    void onMoveEvent() {
        mMoved = true;
    }

    void setClickListener(IListener listener) {
        mListenerRef = new WeakReference<>(listener);
    }

    boolean checkCondition(MotionEvent upEvent) {
        if (upEvent != null) {
            // have moved cancel click
            if (mMoved) {
                mMoved = false;
                return false;
            }
            //ACTION_DOWN  ACTION_UP time too long cancel click
            boolean timeCorrect = upEvent.getEventTime() - time < LONG_CLICK_DELAY;
            time = 0;

            return timeCorrect;
        }
        return false;
    }
}

public interface IListener {
    void onClick(DetectorSeekBar detectorSeekBar);
    void onLongClick(DetectorSeekBar detectorSeekBar);
}

}