How to make TextWatcher wait for some time before doing some action

后端 未结 2 1405
滥情空心
滥情空心 2021-02-08 12:16

I have an EditText to filter the items in the ListView below it, which may contain more than 1000 items usually. The TextWatcher is:

相关标签:
2条回答
  • 2021-02-08 13:00

    How can I make the textWatcher wait for 1-2 secs and if no more input happens after 2 secs, then filter the list.

    As I already said in the comment you should look into using the getFilter() method of the adapter. As that may not be suitable(as you say) try to implement the same mechanism the adapter's filter uses to cancel in between filter inputs.

    private Handler mHandler = new Handler();
    
    public void afterTextChanged(Editable s) {
          mHandler.removeCallbacks(mFilterTask); 
          mHandler.postDelayed(mFilterTask, 2000);
    }
    

    where filterTask is:

    Runnable mFilterTask = new Runnable() {
    
         @Override
         public void run() {
              fillItemList();
         }       
    }
    
    0 讨论(0)
  • 2021-02-08 13:10

    Using RxBinding :

    RxTextView.textChanges(edittext)
                .skipInitialValue()
                .debounce(TIME_TO_WAIT, TimeUnit.MILLISECONDS)
                .subscribe({
                   //do the thing
                })
    }
    
    0 讨论(0)
提交回复
热议问题