其他选项菜单项在展开的搜索视图折叠后不显示

Other options menu items not showing after expanded searchview is collapsed

我有一个简单的 activity 选项菜单设置,代码如下所示。我遇到的问题是,在展开和折叠搜索视图后,带有 app:showAsAction="ifRoom" 的菜单项不会出现,即使工具栏上有空间。另一方面,带有 app:showAsAction="never" 的菜单项会出现。

menu_simple_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_search_white_24dp"
        android:title="@string/action_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always|collapseActionView" />
    <item
        android:id="@+id/action_filter"
        android:icon="@drawable/ic_filter_list_white_24dp"
        android:title="@string/action_filter"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/action_sort"
        android:icon="@drawable/ic_sort_white_24dp"
        android:title="@string/action_sort"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/action_print_all"
        android:icon="@drawable/ic_print_white_24dp"
        android:title="@string/action_print_all"
        app:showAsAction="never" />
</menu>

SimpleActivity.java

public class SimpleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", 
                Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        getMenuInflater().inflate(R.menu.menu_simple_activity, menu);
        MenuItem searchMenuItem = menu.findItem(R.id.action_search);
        final MenuItem filterMenuItem = menu.findItem(R.id.action_filter);
        final MenuItem sortMenuItem = menu.findItem(R.id.action_sort);
        final MenuItem printAllMenuItem = 
        menu.findItem(R.id.action_print_all);

        searchMenuItem.setOnActionExpandListener(new 
        MenuItem.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                filterMenuItem.setVisible(false);
                sortMenuItem.setVisible(false);
                printAllMenuItem.setVisible(false);
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                filterMenuItem.setVisible(true);
                sortMenuItem.setVisible(true);
                printAllMenuItem.setVisible(true);
                return true;
            }
        });
        return true;
    }
}

我该如何解决?

您应该在将项目设置为可见后调用 invalidateOptionsMenu()

来自文档:

Declare that the options menu has changed, so should be recreated. The onCreateOptionsMenu(Menu) method will be called the next time it needs to be displayed.