简体   繁体   English

如何知道操作栏菜单何时展开?

[英]How to know when the action bar menu is expanded?

I have a plain action bar menu like this: 我有一个简单的动作栏菜单,如下所示:

This is what it looks like in Java: 这就是Java中的样子:

getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        //when an item is selected (i.e local/My Places/etc)
        return false;
    }
};

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, new String[] { "Local", "My Places", "Checkins", "Latitude" });

getActionBar().setListNavigationCallbacks(adapter, navigationListener);

The callback for when an item is selected works fine, but I want a callback for when the dropdown is opened/closed. 选择项目时的回调工作正常,但我想要在打开/关闭下拉列表时进行回调。

I've looked at ActionBar.OnMenuVisibilityListener but the following prints nothing in my console. 我查看了ActionBar.OnMenuVisibilityListener但以下内容在我的控制台中没有打印。

ActionBar.OnMenuVisibilityListener listener = new ActionBar.OnMenuVisibilityListener() {
    @Override
    public void onMenuVisibilityChanged(boolean isVisible) {
        System.out.println("hello world!");
    };
};

getActionBar().addOnMenuVisibilityListener(listener);

What can I try next? 我接下来可以尝试什么?

Recently I face the same task. 最近我面临同样的任务。 And I use the same method to show Spinner . 我使用相同的方法来显示Spinner

But to solve this I have to replace this Spinner with custom one. 但要解决这个问题,我必须用自定义的Spinner替换它。 I remove this code and put own Spinner through xml layout of my activity. 我删除此代码并将自己的Spinner放入我的活动的xml布局中。

<com.company.yourapp.View.MenuSpinner
        android:id="@+id/catalogue_menu_spinner"
        android:layout_width="wrap_content"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/catalogue_status_bar"
        android:background="@null"
        android:layout_marginLeft="60dp" />

Class that notify me when menu is showed ( MenuSpinner in code above) I get from this question. 显示菜单时通知我的类( MenuSpinner的代码中的MenuSpinner )我从这个问题得到。

I hope my answer will help someone who like me stuck on this. 我希望我的回答能帮助那些喜欢我的人坚持这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM