How to show Indeterminate Progress bar when Refresh button is pressed in ActionBarSherlock?

时光总嘲笑我的痴心妄想 提交于 2019-12-29 15:03:01

问题


How to show Indeterminate ProgressBar when Refresh button is pressed in ActionBarSherlock and again show Refresh Button when ViewGroup on refreshed?

Update 1: I have a answer here which is incomplete. I am placing a bounty on question so that more developers can help build a good answer which can useful to others in future.

How can we show a Indeterminate ProgressBar which looks like the one shown in the image below


回答1:


It seems like ActionBarSherlock doesn't provide specific method to animate a refresh MenuItem.

What you can do (by using classic android API) is to use the setActionView(int resId) method and give the id of a layout with a ProgressBar in it.

At the beginning of your refresh action just call :

item.setActionView(R.layout.refresh_menuitem);

And when your refresh action is finished call :

item.setActionView(null);

Here is a sample of what your layout file refresh_menuitem.xml can have :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:addStatesFromChildren="true"
              android:focusable="true"
              android:paddingLeft="4dp"
              android:paddingRight="4dp"
              android:gravity="center"
              style="?attr/actionButtonStyle">
    <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="true"
            style="@android:style/Widget.ProgressBar.Small"/>
</LinearLayout> 



回答2:


Here is how you add this kind of indeterminate ProgressBar with a ActionBarSherlock object : (actually it's easier the other one but the progressBar is shown alone and not above a MenuItem)

1 - Put this line in the onCreate() method before the setContentView() call :

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

-> This line specify that you will use the indeterminate ProgressBar function.

2 - Enable the indeterminate ProgressBar by calling :

setSupportProgressBarIndeterminateVisibility(true);

3 - Disable the indeterminate ProgressBar by calling :

setSupportProgressBarIndeterminateVisibility(false);

Remark : Have a look in the sample folder of the ActionBarSherlock folder. I found this code in the following file : JakeWharton-ActionBarSherlock-9598f2b\samples\demos\src\com\actionbarsherlock\sample\demos\IndeterminateProgress.java




回答3:


Here is a complete code:

private static final int menuItemIdRefresh = 10; //class constant
private boolean refresh;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem refreshItem = menu.add(0, menuItemIdRefresh, 0,
            getString(R.string.action_refresh)).setShowAsActionFlags(
            MenuItem.SHOW_AS_ACTION_ALWAYS);

    if (isRefreshing) {
        refreshItem.setActionView(R.layout.indeterminate_progress);
    } else {
        refreshItem.setActionView(null);
        refreshItem.setIcon(R.drawable.ic_refresh);
    }

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case menuItemIdRefresh: {
            //the user has pressed the refresh button
        if (!isRefreshing) {
            isRefreshing = true;
            new RefreshMyViewAsyncTask().execute("");
        }
    }
        break;

    default:
        break;
    }

    return false;
}

One last note, in order to get the above code working you´ll need also call supportInvalidateOptionsMenu(). You can add that to the RefreshMyViewAsyncTask's onPreExecute() method.

Hope this helps to somebody.



来源:https://stackoverflow.com/questions/10905053/how-to-show-indeterminate-progress-bar-when-refresh-button-is-pressed-in-actionb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!