如何使用列表将图标更改为网格
How to change icon using list to grid
我正在使用 StaggeredGridlayoutmanager 将列表更改为网格,并将网格更改为列表,但我想在更改视图时更改图标,比如如果视图处于列表模式我想显示网格图标并且视图处于网格模式我要显示列表模式
case R.id.product_grid:
isViewWithCatalog = !isViewWithCatalog;
supportInvalidateOptionsMenu();
rv.setLayoutManager(isViewWithCatalog ? new LinearLayoutManager(this) : new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
rv.setAdapter(rvAdapter);
break;
我的适配器
@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(isViewWithCatalog ? R.layout.productlistview : R.layout.productgrids, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
return rcv;
}
你可以维护一个布尔变量
case R.id.product_grid:
isGrid = true;
isViewWithCatalog = !isViewWithCatalog;
supportInvalidateOptionsMenu();
rv.setLayoutManager(isViewWithCatalog ? new LinearLayoutManager(this) : new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
rv.setAdapter(rvAdapter);
break;
case R.id.product_list:
isGrid = false;
invalidateOptionMenu() or getActivity.invalidateOptionMenu() // If it is fragment
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_product, menu);
MenuItem list = menu.findItem(R.id.list);
MenuItem grid= menu.findItem(R.id.list);
if(isGrid)
grid.setVisible(true);
list.setvisible(false)
else
list.setVisible(true);
grid.setVisible(false);
return super.onCreateOptionsMenu(menu);
}
我正在使用 StaggeredGridlayoutmanager 将列表更改为网格,并将网格更改为列表,但我想在更改视图时更改图标,比如如果视图处于列表模式我想显示网格图标并且视图处于网格模式我要显示列表模式
case R.id.product_grid:
isViewWithCatalog = !isViewWithCatalog;
supportInvalidateOptionsMenu();
rv.setLayoutManager(isViewWithCatalog ? new LinearLayoutManager(this) : new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
rv.setAdapter(rvAdapter);
break;
我的适配器
@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(isViewWithCatalog ? R.layout.productlistview : R.layout.productgrids, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
return rcv;
}
你可以维护一个布尔变量
case R.id.product_grid:
isGrid = true;
isViewWithCatalog = !isViewWithCatalog;
supportInvalidateOptionsMenu();
rv.setLayoutManager(isViewWithCatalog ? new LinearLayoutManager(this) : new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
rv.setAdapter(rvAdapter);
break;
case R.id.product_list:
isGrid = false;
invalidateOptionMenu() or getActivity.invalidateOptionMenu() // If it is fragment
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_product, menu);
MenuItem list = menu.findItem(R.id.list);
MenuItem grid= menu.findItem(R.id.list);
if(isGrid)
grid.setVisible(true);
list.setvisible(false)
else
list.setVisible(true);
grid.setVisible(false);
return super.onCreateOptionsMenu(menu);
}