Created
June 25, 2016 21:45
-
-
Save kaushikgopal/ea0d81206815734a9964eb984c6d5fb7 to your computer and use it in GitHub Desktop.
Blog post snippet
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
// the below code is inside a TextWatcher | |
// which implements the onTextChanged method | |
// I've simplified it to only highlight the parts we're | |
// interested in | |
private long lastChange = 0; | |
@Override | |
public void onTextChanged(final CharSequence chars, | |
int start, int before, int count) { | |
// The handler is spawned from the UI thread | |
new Handler().postDelayed( | |
// argument 1 for postDelated = message to be sent | |
new Runnable() { | |
@Override | |
public void run() { | |
if (noChangeInText_InTheLastFewSeconds()) { | |
searchAndPopulateListView(chars.toString()); // logic | |
} | |
} | |
}, | |
// argument 2 for postDelated = delay before execution | |
300); | |
lastChange = System.currentTimeMillis(); | |
} | |
private boolean noChangeInText_InTheLastFewSeconds() { | |
return System.currentTimeMillis() - lastChange >= 300 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment