【问题标题】:Show progress spinner (refresh) on ActionBar?在 ActionBar 上显示进度微调器(刷新)?
【发布时间】:2012-01-30 02:00:31
【问题描述】:

我正在使用操作栏。我想在标题栏上有一个刷新进度微调器,如果我将它设置为旋转 - 否则隐藏它。这可能吗?:

// 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 上,除非它“正在进行”/正在旋转。

谢谢

【问题讨论】:

    标签: android android-actionbar


    【解决方案1】:

    抱歉没有代码标签,从手机发帖...

    这是来自 ActionbarSherlock(如果您没有遇到过,Google 允许在 prehoneycomb 中支持操作栏)

    在主Activity的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,而不仅仅是 true/false......

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

    【讨论】:

    • 好的,谢谢,我会在那里挖掘一下。 SDK 提供的示例并没有我最初想象的那么好。他们正在使用 Window.FEATURE_CUSTOM_TITLE ,它的效果是在您想要完全绘制之前向用户显示一个标题栏,它看起来真的很不专业。叹息。
    • @Jake Wharton 我正在努力解决这个问题。当refresh 按钮被按下时,回调onOptionsItemSelected 被调用。根据 MartinS 的建议,我们在哪里调用 setProgressBarIndeterminateVisibility
    • 如果您使用的是 ActionBarSherlock,请确保使用 setSupportProgressBarIndeterminateVisibility()。否则,您的进度指示器会一直旋转,而您将不知道为什么。
    • supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);(至少在使用ActionBarCompat时)
    • 对于 ActionBarCompat 你还需要supportRequestWindowFeature(Window.FEATURE_PROGRESS); 否则你会得到 NullPointerException (详情:stackoverflow.com/questions/18192148/…
    【解决方案2】:

    如果你从 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;
                }
            });
    }
    

    【讨论】:

    猜你喜欢
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多