Android: 如果 RecyclerView 为空,如何隐藏 SearchView 图标?

Android: how do I hide SearchView icon if RecyclerView is empty?

我有一个 RecyclerView 有两个可能 Views。第一个 View 加载名为 emptyView 的默认布局。当用户创建新的 CardView 时,第二个 View 会替换第一个 View。我希望为 RecyclerView.

添加搜索功能

我不想在 RecyclerView 为空时在 AppBar 上显示 action_search 图标;也就是说,在创建任何 CardViews 之前。在用户创建 CardView 之后,我希望 action_search 图标出现在 AppBar 上。那么我如何 link SearchView 代码和搜索功能的 setOnActionExpandListener 来隐藏第一个视图的搜索图标?然后创建一个CardView后,识别出emptyView !=nullRecyclerViewAdapter !=null来显示第二个视图?

Activity.java

...  

// setEmptyView() in the EmptyRecyclerView subclass, calls checkIfEmpty()
// empty_view is an empty view (Linear Layout) that is shown when the 
// RecyclerView is empty (the default case).
   recyclerView.setEmptyView(findViewById(R.id.empty_view)); 
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {        
    getMenuInflater().inflate(R.menu.cardview_menu, menu);
    final MenuItem item = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
    searchView.setOnQueryTextListener(this);

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

EmptyRecyclerView.java
...
public class EmptyRecyclerView extends RecyclerView {

private View emptyView;
...
void checkIfEmpty() {
    if (emptyView != null && getAdapter() != null) {
        final boolean emptyViewVisible =
                getAdapter().getItemCount() == 0;
        emptyView.setVisibility(emptyViewVisible ? VISIBLE : GONE);
        setVisibility(emptyViewVisible ? GONE : VISIBLE);
    }

    public void setEmptyView(View emptyView) {
    this.emptyView = emptyView;
    checkIfEmpty();
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) { 

if (getAdapter() == null || getAdapter().getItemCount() == 0) {
    return super.onCreateOptionsMenu(menu);
}

getMenuInflater().inflate(R.menu.cardview_menu, menu);
final MenuItem item = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);

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

创建卡片视图后,在 recyclerView 中设置数据后,只需调用 invalidateOptionsMenu();

更新:

如果您的菜单列表中有多项,而您必须 hide/show 只有一项:

@Override
public boolean onCreateOptionsMenu(Menu menu) { 

getMenuInflater().inflate(R.menu.cardview_menu, menu);
final MenuItem item = menu.findItem(R.id.action_search);

if (getAdapter() == null || getAdapter().getItemCount() == 0) {
    item.setVisible(false);
}else {
    item.setVisible(true);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
    searchView.setOnQueryTextListener(this);
}
MenuItemCompat.setOnActionExpandListener(item,
    new MenuItemCompat.OnActionExpandListener(){
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            return true;
        }
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return true;
        }
    });
return true;
}