Skip to content

Instantly share code, notes, and snippets.

@m7mdra
Last active April 25, 2018 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m7mdra/9f5d7c7ef321e29206d4309306bec376 to your computer and use it in GitHub Desktop.
Save m7mdra/9f5d7c7ef321e29206d4309306bec376 to your computer and use it in GitHub Desktop.
A debounce searchview query listener. [DO NOT USE THIS , TRY TO USE RXJAVA]
package com.reqabaweb.healthcontrol.ui;
import android.os.SystemClock;
import android.support.v7.widget.SearchView;
import android.util.Log;
public abstract class DebouncedQueryTextListener implements SearchView.OnQueryTextListener {
private static final String TAG = "DebouncedOnQueryTextLis";
private static final long THRESHOLD_MILLIS = 500L;
private long lastClickMillis;
private int calls = 0;
protected DebouncedQueryTextListener() {
}
public abstract void onQueryDebounce(String text);
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
long now = SystemClock.elapsedRealtime();
if (now - lastClickMillis > THRESHOLD_MILLIS) {
calls += 1;
onQueryDebounce(newText);
Log.d(TAG, "onQueryTextChange: " + calls);
}
lastClickMillis = now;
return true;
}
}
@m7mdra
Copy link
Author

m7mdra commented Jun 6, 2017

implementation is pretty simple and straightforward

 searchView.setOnQueryTextListener(new DebouncedQueryTextListener() {
            @Override
            public void onQueryDebounce(String text) {
                query(text); // do something with the query
            }
        });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment