使用SearchView延迟对SearchView.OnQueryTextListener中的onQueryTextChange()的调用 [英] Delay call to onQueryTextChange() in SearchView.OnQueryTextListener with SearchView
问题描述
在我的应用程序中使用 SearchView
.
Using a SearchView
in my application.
无论如何,我可以延迟对onQueryTextChange()方法的调用.例如,当用户键入一个字符序列时,他必须等待此方法被调用.
Is there anyway in which I can make the call to onQueryTextChange() method delayed. Like, when user type a sequence of characters, he must wait before this method gets called.
此等待应该不取决于键入的字符数,但是在方法被点击之前需要稍作停顿.
This wait should not depend on number of characters typed yet but a small pause is required before method being hit.
我想暂停一下,因为当用户在搜索视图中键入内容时,将向服务器发出带有字符串的请求,以请求匹配的数据,然后根据建议填充ListView.
I want to pause because, as the user types into the search view, a request with the string will be made to server to request the matched data, and then I will populate my ListView according to suggestions.
活动信息(如果需要):
实现 SearchView.OnQueryTextListener
.
Activity information (if required) :
Implements SearchView.OnQueryTextListener
.
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView)MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(this);
推荐答案
要延迟对服务器的调用,请在onQueryTextChange方法中使用以下代码,变量mQueryString和mHandler必须是类变量.还检查mHandler!= null
To delay the call to your server, use the following code in your onQueryTextChange method, the variables mQueryString and mHandler must be class variables. also check mHandler!=null
@Override
public boolean onQueryTextChange(String searchTerm) {
mQueryString = searchTerm;
mHandler.removeCallbacksAndMessages(null);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
//Put your call to the server here (with mQueryString)
}
}, 300);
return true;
}
这篇关于使用SearchView延迟对SearchView.OnQueryTextListener中的onQueryTextChange()的调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!