使用SearchView延迟调用SearchView.OnQueryTextListener中的onQueryTextChange()

时间:2015-05-05 08:19:53

标签: android searchview

在我的应用程序中使用SearchView

无论如何,我可以延迟调用onQueryTextChange()方法。就像,当用户键入一系列字符时,他必须等待此方法被调用。

此等待取决于尚未输入的字符数,但在方法被点击之前需要暂停一小段时间。

我想暂停,因为当用户键入搜索视图时,将向服务器发出带有字符串的请求以请求匹配的数据,然后我将根据建议填充我的ListView。

活动信息(如果需要):
实现SearchView.OnQueryTextListener

    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView)MenuItemCompat.getActionView(searchItem);
    searchView.setOnQueryTextListener(this);

5 个答案:

答案 0 :(得分:28)

要延迟对服务器的调用,请在onQueryTextChange方法中使用以下代码,变量mQueryString和mHandler必须是类变量。 还要检查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;
}

答案 1 :(得分:6)

这应该对你有帮助,你的班级需要实现" SearchView.OnQueryTextListener"和" cntr"必须在你的班级中声明

对于常规用户输入,这已经是闪烁的,如果你想等待更多,只需提高" waitingTime"。

请求应位于" onFinish"

    private int waitingTime = 200;
    private CountDownTimer cntr;

    @Override
    public boolean onQueryTextChange(String newText) {
    if(cntr != null){
        cntr.cancel();
    }
    cntr = new CountDownTimer(waitingTime, 500) {

        public void onTick(long millisUntilFinished) {
            Log.d("TIME","seconds remaining: " + millisUntilFinished / 1000);
        }

        public void onFinish() {
            Log.d("FINISHED","DONE");
        }
    };
    cntr.start();
    return false;
}

答案 2 :(得分:1)

使用SearchView延迟调用SearchView.OnQueryTextListener中的onQueryTextChange()

private long delay = 2000; 
private long editTextLastWord = 0;
privaste  SearchView searchView;
Handler handler = new Handler();

private Runnable runnable = new Runnable() {
    public void run() {
        if (System.currentTimeMillis() > (editTextLastWord + delay - 500)) {

          perFormTask();
        }
    }
};
 searchView=(SearchView) findViewById(R.id.searchView);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
 if (s.length() > 2) {              
          editTextLastWord = System.currentTimeMillis();
          handler.postDelayed(runnable, delay);
        } else {

        }
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
  handler.removeCallbacks(runnable);
            }
        });

答案 3 :(得分:0)

你可以像这样使用Kotlin协同程序。 声明倒计时作业

private lateinit var textChangeCountDownJob: Job

然后onQueryTextChange:

    override fun onQueryTextChange(newText: String): Boolean {

        if(::textChangeCountDownJob.isInitialized)
            textChangeCountDownJob.cancel()

        textChangeCountDownJob = launch(UI) {
            delay(800)
        }

        return false
    }

答案 4 :(得分:0)

1)创建抽象类:

public abstract class DelayedOnQueryTextListener implements SearchView.OnQueryTextListener {

    private Handler handler = new Handler();
    private Runnable runnable;

    @Override
    public boolean onQueryTextSubmit(String s) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String s) {
        handler.removeCallbacks(runnable);
        runnable = () -> onDelayerQueryTextChange(s);
        handler.postDelayed(runnable, 400);
        return true;
    }

    public abstract void onDelayerQueryTextChange(String query);
}

2)像这样设置:

searchView.setOnQueryTextListener(new DelayedOnQueryTextListener() {
    @Override
    public void onDelayerQueryTextChange(String query) {
        // Handle query
    }
});