簡體   English   中英

ActionBar刷新按鈕

[英]ActionBar refresh button

我在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()說“無法解析方法”,然后在“出現”中表示“無法應用Fragment” ...

編輯*

我以為我的問題集中在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);
    }
}

有解決這個問題的主意嗎?

如果您是通過“活動”致電的

@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);
    }

像這樣使用它:

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:添加的所有代碼將執行該功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM