在SearchView中调整onQueryTextChange [英] Throttle onQueryTextChange in SearchView

查看:1013
本文介绍了在SearchView中调整onQueryTextChange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限制 onQueryTextChange 的最佳方法是什么,以便我的 performSearch()方法每次只调用一次第二次而不是每次用户输入?

What's the best way to "throttle" onQueryTextChange so that my performSearch() method is called only once every second instead of every time the user types?

public boolean onQueryTextChange(final String newText) {
    if (newText.length() > 3) {
        // throttle to call performSearch once every second
        performSearch(nextText);
    }
    return false;
}


推荐答案

我最终得到了一个解决方案类似于下面。这样它应该每半秒触发一次。

I ended up with a solution similar to below. That way it should fire once every half second.

        public boolean onQueryTextChange(final String newText) {

            if (newText.length() > 3) {

                if (canRun) {
                    canRun = false;
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            canRun = true;
                            handleLocationSearch(newText);
                        }
                    }, 500);
                }
            }

            return false;
        }

这篇关于在SearchView中调整onQueryTextChange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆