How to switch between two menu icons

时间:2016-04-04 17:04:14

标签: android menuitem

I have menu items that are inflated on my toolbar.When one menu item icon is clicked i would like to switch that with another icon and vice versa. I'm doing this so that i can switch between listview and gridview.

What i have tried is to recreate the menu again with a different menu.xml. But the app crashes when i call onCreateOptionsMenu(menu) from onOptionsItemSelected(MenuItem item){}.

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       int id = item.getItemId();
       if (id == R.id.action_grid) {
            listView.setVisibility(View.INVISIBLE);
            gv.setVisibility(View.VISIBLE);
         //i try to recreate menu again
          onCreateOptionsMenu(menu)

        }
        if (id == R.id.action_list) {
            listView.setVisibility(View.VISIBLE);
            gv.setVisibility(View.INVISIBLE);
           //i try to recreate menu again
           onCreateOptionsMenu(menu)
        }


        return super.onOptionsItemSelected(item);
    }

Is there a way to achieve this?

3 个答案:

答案 0 :(得分:1)

Menu optionsMenu;

@Override
  public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.refresh_menu, menu);
    optionsMenu = menu; 
}

**Somewhere in code**
final MenuItem refreshItem = optionsMenu
          .findItem({ID OF MENU Item});
refreshItem.setIcon({New Icon Resource});

答案 1 :(得分:1)

You don't need to recreate menu everytime, Just handle MenuItems

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    String title = item.getTitle().toString();
    switch (title) {
        case TITLE_GRIDVIEW:
            item.setTitle(TITLE_LISTVIEW);
            item.setIcon(R.drawable.listview_icon);
            //switch to gridview
            return true;
        case TITLE_LISTVIEW:
            item.setTitle(TITLE_GRIDVIEW);
            item.setIcon(R.drawable.gridview_icon);
            //switch to listview
            return true;
    }
    return false;
}

It works based on the binded title to your item, In this way you just need one item in your menu to switch between listview and gridview and this items acts like a toggle button.

答案 2 :(得分:0)

Instead of trying to re-create the menu, just change the icon and maintain state with a boolean flag:

boolean flag = true;

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_grid_list_toggle) {
        if (boolean) {
            // Show grid view
            item.setIcon(R.drawable.ic_grid);
            listView.setVisibility(View.INVISIBLE);
            gv.setVisibility(View.VISIBLE);
        } else {
            // Show list view
            item.setIcon(R.drawable.ic_list);
            listView.setVisibility(View.VISIBLE);
            gv.setVisibility(View.INVISIBLE);
        }
        flag = !flag; // toggle value on every click
    }
    return super.onOptionsItemSelected(item);
}