我有一个RecyclerView
,其中有两个Views
。第一个View
加载名为emptyView
的默认布局。当用户创建新的View
时,第二个View
会替换第一个CardView
。我希望将搜索功能添加到RecyclerView
。
当action_search
为空时,我不想在AppBar
上显示RecyclerView
图标;也就是说,在创建任何CardViews
之前。用户创建CardView
后,我希望action_search
图标显示在AppBar
上。那么如何链接搜索功能的SearchView
代码和setOnActionExpandListener
来隐藏第一个视图的搜索图标?然后在创建CardView
后,识别出emptyView !=null
和RecyclerViewAdapter !=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();
}
}
答案 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;
}