setOnActionExpandListener和OnActionExpandListener不起作用

问题描述 投票:1回答:1

您好Stackoverflow团队,

以下是我的搜索视图代码:

private void setupSearchView(MenuItem searchItem) {
    MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            searchQuery = null;
            updateData();
            return true;
        }
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return true;
        }
    });
    SearchManager searchManager = (SearchManager) (getSystemService(Context.SEARCH_SERVICE));
    searchView = (SearchView) searchItem.getActionView();
    if (searchView != null) {
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        searchView.clearFocus();
        searchView.setOnQueryTextListener(onQueryTextListener);
    }
}

不推荐使用setOnActionExpandListener和OnActionExpandListener,它不起作用。有人可以帮我修复代码吗?提前致谢

android search actionlistener
1个回答
0
投票

此方法在API级别26.1.0中已弃用。

而不是MenuItemCompat.OnActionExpandListener

直接使用MenuItem.OnActionExpandListener。

示例代码

MenuItem item = menu.findItem(R.id.action_order);
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
     @Override
      public boolean onMenuItemActionExpand(MenuItem menuItem) {
          isSearch = true;
          return true;
      }

      @Override
      public boolean onMenuItemActionCollapse(MenuItem menuItem) {
           isSearch = false;
           return true;
      }
});

你也可以得到帮助this

© www.soinside.com 2019 - 2024. All rights reserved.