Delay call to onQueryTextChange() in SearchView.OnQueryTextListener with SearchView

前端 未结 5 1309
闹比i
闹比i 2021-02-07 05:12

Using a SearchView in my application.

Is there anyway in which I can make the call to onQueryTextChange() method delayed. Like, when user type a sequence o

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-07 06:03

    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;
    }
    

提交回复
热议问题