I am changing list to grid and grid to list using StaggeredGridlayoutmanager, but i want to change icon while changing the view,,like if view is in list mode i want to display grid icon and and view is in grid mode i want to display list mode
case R.id.product_grid:
isViewWithCatalog = !isViewWithCatalog;
supportInvalidateOptionsMenu();
rv.setLayoutManager(isViewWithCatalog ? new LinearLayoutManager(this) : new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
rv.setAdapter(rvAdapter);
break;
MyAdapter
@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;
}
You can maintain one boolean variable
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);
}