How do I open the SearchView programmatically?

后端 未结 8 1633
既然无缘
既然无缘 2020-12-02 09:19

There is this widget for the ActionBar which called \'SearchView\'. When it\'s not in use, it looks like this:

\

8条回答
  •  Happy的楠姐
    2020-12-02 09:22

    I was searching for a solution to expand SearchView programmatically because I need to restore expand state. No one solution was worked for me. Except using Handler().post(). I know it's not a good practice to use post(). But I'm working on legacy project and this workaround is acceptable for me.

    Initialize variables block

    val searchMenuItem = menu.findItem(R.id.menu_search_item)
    val searchView = searchMenuItem.actionView as SearchView
    

    How to expand SearchView:

    Handler().post {
       searchMenuItem.expandActionView()
    }
    

    Reason why post() helps: if your app is not well written it may doing too much work on UI thread. post() ensures that your block of code will be executed when UI thread will be available for execution. Which means if you are using this workaround you can notice small delay between SearchView expanding

提交回复
热议问题