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

时间:2017-01-16 04:35:00

标签: android android-recyclerview menuitem

我有一个RecyclerView,其中有两个Views。第一个View加载名为emptyView的默认布局。当用户创建新的View时,第二个View会替换第一个CardView。我希望将搜索功能添加到RecyclerView

action_search为空时,我不想在AppBar上显示RecyclerView图标;也就是说,在创建任何CardViews之前。用户创建CardView后,我希望action_search图标显示在AppBar上。那么如何链接搜索功能的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();
    }
}

1 个答案:

答案 0 :(得分:2)

@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();

<强>更新

如果菜单列表中有多个项目,则必须隐藏/只显示其中一个项目:

@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;
}