Last active
April 25, 2018 11:39
-
-
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]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
implementation is pretty simple and straightforward