Last active
February 6, 2021 08:47
-
-
Save gotev/46e4eaf2bfb522512d4c0e2c73e7d23b to your computer and use it in GitHub Desktop.
Android Throttled Search
This file contains hidden or 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
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.SearchView; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
import android.widget.EditText; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
/** | |
* Throttles a search action by an amount of milliseconds. | |
* | |
* @author Aleksandar Gotev | |
*/ | |
public class ThrottledSearch { | |
public interface Delegate { | |
void onThrottledSearch(String searchTerm); | |
} | |
private static final int DEFAULT_DELAY = 400; | |
private int mDelayMilliseconds; | |
private final Delegate mDelegate; | |
private String mSearchTerm; | |
private Timer mTimer; | |
private AppCompatActivity mActivity; | |
public ThrottledSearch(AppCompatActivity activity, Delegate delegate, int milliseconds) { | |
mActivity = activity; | |
mDelayMilliseconds = milliseconds; | |
mDelegate = delegate; | |
mSearchTerm = ""; | |
} | |
public ThrottledSearch(AppCompatActivity activity, Delegate delegate) { | |
this(activity, delegate, DEFAULT_DELAY); | |
} | |
public void onTextChanged(CharSequence charSequence) { | |
if (mTimer != null) | |
mTimer.cancel(); | |
mTimer = new Timer(); | |
mSearchTerm = charSequence.toString(); | |
mTimer.schedule(new TimerTask() { | |
@Override | |
public void run() { | |
mActivity.runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
mDelegate.onThrottledSearch(mSearchTerm); | |
} | |
}); | |
} | |
}, mDelayMilliseconds); | |
} | |
public void attachTo(final EditText editText) { | |
editText.addTextChangedListener(new TextWatcher() { | |
@Override | |
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { | |
} | |
@Override | |
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { | |
ThrottledSearch.this.onTextChanged(charSequence); | |
} | |
@Override | |
public void afterTextChanged(Editable editable) { | |
} | |
}); | |
} | |
public void attachTo(final SearchView searchView) { | |
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { | |
@Override | |
public boolean onQueryTextSubmit(String query) { | |
return true; | |
} | |
@Override | |
public boolean onQueryTextChange(String newText) { | |
ThrottledSearch.this.onTextChanged(newText); | |
return true; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
alternatively, you can create a new object:
and then call
search.onTextChanged("something");
in your own edit text listener.You can also set the throttle amount in milliseconds in the constructor (by default it's 400ms):