2012-01-30 56 views
30

我正在使用ActionBar。我希望在標題欄上有一個刷新進度微調器,如果我將它設置爲旋轉 - 否則將其隱藏。這是可能的嗎?:在ActionBar上顯示進度微調(刷新)?

// My menu has a refresh item, but it shouldn't be visible on the 
// actionbar unless it's spinning. 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:id="@+id/menu_refresh" 
    android:title="@string/refresh" 
    android:icon="@drawable/ic_action_refresh" /> 
</menu> 

... 

// When I need to show some work being done on my activity, 
// can I somehow now make the spinner associated with the 
// refresh item become visible on the action bar? 
getActionBarHelper().setRefreshActionItemState(true); 

我不希望它在ActionBar,除非它「正在進行」/旋轉。

感謝

回答

75

道歉無碼標籤,可以從手機發布...

這是ActionbarSherlock(谷歌,如果你還沒有越過它來了,讓動作條預蜂窩狀支持)

在主要活動的onCreate

// This has to be called before setContentView and you must use the 
// class in android.support.v4.view and NOT android.view 

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 

要顯示在操作欄/隱藏進度。與actionbarsherlock請注意,你必須使用boolean.TRUE/FALSE,不只是真/假.........

if (getSupportLoaderManager().hasRunningLoaders()) { 
    setProgressBarIndeterminateVisibility(Boolean.TRUE); 
} else { 
    setProgressBarIndeterminateVisibility(Boolean.FALSE); 
} 
+0

好謝謝我會在那裏挖一下。隨SDK提供的示例不如我最初的想法。他們使用Window.FEATURE_CUSTOM_TITLE,它可以在用戶完全繪製之前向用戶顯示標題欄,這看起來確實不專業。嘆。 – user291701 2012-01-30 14:02:32

+0

@Jake Wharton我正在努力解決這個問題。當按下'refresh'按鈕時,調用回調'onOptionsItemSelected'。在MartinS的建議下,我們在哪裏調用setProgressBarIndeterminateVisibility? – 2012-06-05 20:59:50

+19

如果您使用的是ActionBarSherlock,請確保使用setSupportProgressBarIndeterminateVisibility()。否則,你的進度指示器會一直旋轉,你不知道爲什麼。 – 2012-11-11 21:14:39

3

如果從ActionBarActivity延伸,試試這個:

public class MainActivity extends ActionBarActivity { 

    boolean showUp=true; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
     setContentView(R.layout.activity_main); 
     setSupportProgressBarIndeterminateVisibility(Boolean.TRUE); 

     Button b = (Button) findViewById(R.id.myButton); 
     b.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       if(showUp){ 
        setSupportProgressBarIndeterminateVisibility(Boolean.FALSE); 
       }else { 
        setSupportProgressBarIndeterminateVisibility(Boolean.TRUE); 
       } 
       showUp=!showUp; 
      } 
     }); 
} 
+0

「Window」屬於哪個包? – Hesam 2014-12-17 04:09:39

+3

支持庫v21不支持這個,所以請注意。 ://stackoverflow.com/questions/26443490/appcompat-show-progress-in-action-bar-causes-npe和https://chris.banes.me/2014/10/17/appcompat-v21/#comment- 1642002459 – indyfromoz 2014-12-27 23:23:41