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

前端 未结 5 1310
闹比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条回答
  •  抹茶落季
    2021-02-07 05:50

    This should help you, your class need to implement "SearchView.OnQueryTextListener" and "cntr" must be declarated in your class

    This is already twinked for a regular user typing, if you want to wait more, just raise the "waitingTime".

    The request should be inside the "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;
    }
    

提交回复
热议问题