ActionBar刷新按钮

时间:2015-01-07 14:48:06

标签: java android android-studio android-actionbar

我在ActionBar上添加了一个Refresh按钮,但问题是我的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解析方法”,然后在“显示”中“在片段中无法应用”...

EDITED *

我认为我的问题主要集中在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);
    }
}

有什么想法解决这个问题?

2 个答案:

答案 0 :(得分:2)

如果您从“活动使用”中致电

@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方法夸大菜单。

并且,正如评论所说,只需从同一方法返回true

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

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

答案 1 :(得分:1)

像这样使用:

<强> 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>

在上面的代码的帮助下,您将在操作栏的顶部获得一个菜单项。然后您在case R.id.search:内添加的代码将执行该功能。