什么是“节流”onQueryTextChange
以便我的 performSearch()
方法每秒只调用一次而不是每次用户键入时调用的最佳方法?
public boolean onQueryTextChange(final String newText) {
if (newText.length() > 3) {
// throttle to call performSearch once every second
performSearch(nextText);
}
return false;
}
最佳答案
如果您使用的是 Kotlin 和协程,您可以执行以下操作:
var queryTextChangedJob: Job? = null
...
fun onQueryTextChange(query: String) {
queryTextChangedJob?.cancel()
queryTextChangedJob = launch(Dispatchers.Main) {
delay(500)
performSearch(query)
}
}
关于java - 在 SearchView 中限制 onQueryTextChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34955109/