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

前端 未结 5 1312
闹比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:54

    1) Create abstract class:

    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) Set it like this:

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

提交回复
热议问题