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

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

    You can use Kotlin coroutines like this. Declare the countdown job

    private lateinit var textChangeCountDownJob: Job
    

    And then onQueryTextChange:

        override fun onQueryTextChange(newText: String): Boolean {
    
            if(::textChangeCountDownJob.isInitialized)
                textChangeCountDownJob.cancel()
    
            textChangeCountDownJob = launch(UI) {
                delay(800)
            }
    
            return false
        }
    

提交回复
热议问题