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

时间:2017-11-28 15:30:47

标签: android searchview android-optionsmenu

我有一个简单的活动,选项菜单设置如下所示。我遇到的问题是,在展开和折叠搜索视图后,即使工具栏上有空间,也不会显示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;
    }
}

我该如何解决?

1 个答案:

答案 0 :(得分:3)

将项目设置为可见后,您应该致电invalidateOptionsMenu()

来自文档:

  

声明选项菜单已更改,因此应重新创建。该   onCreateOptionsMenu(Menu)方法将在下次需要时调用   待显示。