Is there any listener on Android SearchView to notify if SearchView is expanded and ready to take input?

前端 未结 4 1552
攒了一身酷
攒了一身酷 2021-02-13 11:10

I want to show some default suggestions in SearchView when User hasn\'t typed anything. I am setting my custom suggestion adapter manually using matrix cursor. I tried setting

4条回答
  •  失恋的感觉
    2021-02-13 11:49

    You can set an expand/collapse listener on the MenuItem like this:

    menuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return true;
        }
    
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            return true;
        }
    });
    

    This listener was introduced with API level 14, so for backwards compatibility you have to use the v4 support library. If you do then you have to set the expand/collapse listener like this:

    MenuItemCompat.setOnActionExpandListener(this.searchItem, new MenuItemCompat.OnActionExpandListener() {
    
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return true;
        }
    
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            return true;
        }
    });
    

提交回复
热议问题