2012-01-30 52 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