ActionBar 刷新按钮

ActionBar refresh button

我在我的 ActionBar 上添加了一个刷新按钮,但问题是我的 ActionBar 上总是有这个刷新按钮。我听说我必须创建一个

这是我添加图标的地方:

 <item android:id="@+id/ofertasRefresh"
    android:icon="@drawable/ic_action_refresh"
    android:title="Refresh"
    android:alphabeticShortcut="r"
    android:orderInCategory="1"
    android:showAsAction="always" />

然后我在 Activity 上添加了这段代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    this.optionsMenu = menu;
    MenuInflater inflater = getMenuInflater(); //ERROR<-----------
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu); // in Fragment cannot be applied <------------
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.airport_menuRefresh:

            // Complete with your code
            return true;
    }
    return super.onOptionsItemSelected(item);
}
public void setRefreshActionButtonState(final boolean refreshing) {
    if (optionsMenu != null) {
        final MenuItem refreshItem = optionsMenu
                .findItem(R.id.ofertasRefresh);
        if (refreshItem != null) {
            if (refreshing) {
                refreshItem.setActionView(R.layout.actionbar_indeterminate_progress);
            } else {
                refreshItem.setActionView(null);
            }
        }
    }
}

getMenuInflater() 上说 "Cannont resolve the method" 然后在 "appears" 上说 "in Fragment cannot be applied"...

已编辑*

我认为我的问题集中在 MyActivity.java 然后我粘贴了 OnCreateOptionsMenuonOptionsItemSelected

的代码

MyActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle action bar actions click
    switch (item.getItemId()) {
    case R.id.action_settings:
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

有解决这个问题的办法吗?

如果您是从 Activity 拨打电话,请使用

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //this.optionsMenu = menu;
    //MenuInflater inflater = getMenuInflater(); //ERROR<-----------
    getMenuInflater().inflate(R.menu.main, menu);
    //return super.onCreateOptionsMenu(menu); // in Fragment cannot be applied <------------
    return true;
}

onCreateOptionsMenu 方法中扩充菜单。

而且,正如评论所说,只是 return true 来自相同的方法。

如果从片段调用,方法看起来会略有不同:

    @Override
    public void onCreateOptionsMenu(
        Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.main, menu);
    }

这样使用:

MainActivity.java:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
       switch (item.getItemId()) 
       {
         case R.id.search:  // it is going to refer the search id name in main.xml

         //add your code here

            return true;
         default:
            return super.onOptionsItemSelected(item);
       }
    }

main.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" >

    <item
        android:id="@+id/search"
        android:icon="@drawable/ic_action_settings"  --> add a icon in drawable that will display in the top of the action bar.
        android:title="@string/search"
        android:showAsAction="always"/>

</menu>

在上述代码的帮助下,您将在操作 bar.Then 顶部获得一个菜单项,无论您在 case R.id.search: 中添加什么代码来执行该功能。