在MenuItem上显示进度条,即使它处于分割操作栏模式

时间:2014-03-18 22:46:25

标签: android android-actionbar android-progressbar android-menu android-actionbar-compat

我正在检查如何将菜单项的图标更改为进度条加载动画。我试过

<item android:id="@+id/action_favorite"
    android:icon="@drawable/ic_action_heart"
    clasificados:showAsAction="always|collapseActionView"
    clasificados:actionViewClass="android.widget.ProgressBar"/>

我正在使用分割操作栏模式。当我点击它时,图标消失,进度条显示在顶栏上。

我需要改变什么?

2 个答案:

答案 0 :(得分:18)

您需要动态添加和删除操作视图。这是一个简单的例子

<item
    android:id="@+id/action_favorite"
    android:icon="@drawable/ic_action_heart"
    android:showAsAction="always" />

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_favorite:
            item.setActionView(new ProgressBar(this));
            item.getActionView().postDelayed(new Runnable() {

                @Override
                public void run() {
                    item.setActionView(null);
                }
            }, 1000);
            return true;
        default:
            return false;
    }
}

关于gif的速度,它只是我记录的比特率。 Example gif

答案 1 :(得分:0)

来自图书馆UCrop https://github.com/Yalantis/uCrop

ucrop_loader_circle_path.xml

<objectAnimator
    android:duration="@integer/ucrop_progress_loading_anim_time"
    android:propertyName="trimPathEnd"
    android:repeatCount="-1"
    android:valueFrom="0"
    android:valueTo="1"
    android:valueType="floatType"/>

<objectAnimator
    android:duration="@integer/ucrop_progress_loading_anim_time"
    android:propertyName="strokeAlpha"
    android:repeatCount="-1"
    android:valueFrom="1"
    android:valueTo="0"
    android:valueType="floatType"/>

ucrop_loader_circle_scale.xml

<objectAnimator
    android:duration="@integer/ucrop_progress_loading_anim_time"
    android:propertyName="scaleX"
    android:repeatCount="-1"
    android:valueFrom="0.666"
    android:valueTo="1.0"
    android:valueType="floatType"/>

<objectAnimator
    android:duration="@integer/ucrop_progress_loading_anim_time"
    android:propertyName="scaleY"
    android:repeatCount="-1"
    android:valueFrom="0.666"
    android:valueTo="1.0"
    android:valueType="floatType"/>

<objectAnimator
    android:duration="@integer/ucrop_progress_loading_anim_time"
    android:propertyName="rotation"
    android:repeatCount="-1"
    android:valueFrom="0"
    android:valueTo="360"
    android:valueType="floatType"/>

ucrop_vector_loader.xml

<group
    android:name="circleGroup"
    android:pivotX="96.0"
    android:pivotY="96.0"
    android:rotation="0">

    <path
        android:name="circlePath"
        android:pathData="M96,5.56405C145.946,5.56405 186.436,46.0536 186.436,96C186.436,145.946 145.946,186.436 96,186.436C46.0536,186.436 5.56405,145.946 5.56405,96C5.56405,46.0536 46.0536,5.56405 96,5.56405Z"
        android:strokeAlpha="1"
        android:strokeColor="@color/ucrop_color_toolbar_widget"
        android:strokeWidth="13"
        android:trimPathEnd="0.7"/>

</group>

ucrop_vector_loader_animated.xml

<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
                 android:drawable="@drawable/ucrop_vector_loader">
    <target
        android:name="circleGroup"
        android:animation="@anim/ucrop_loader_circle_scale"/>
    <target
        android:name="circlePath"
        android:animation="@anim/ucrop_loader_circle_path"/>

</animated-vector>

menu_playlist.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/item_action_favorite_add"
        android:title="Add"
        app:showAsAction="always"
        android:icon="@drawable/ic_playlist_favorite_off"
        />


    <item
        android:id="@+id/menu_loader"
        android:enabled="false"
        android:icon="@drawable/ucrop_vector_loader_animated"
        android:title=""
        app:showAsAction="always"/>
</menu>

活动:

boolean mShowProgressMenuItemLoader = false;

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_playlist, menu);
        MenuItem menuItemLoader = menu.findItem(R.id.menu_loader);
        Drawable menuItemLoaderIcon = menuItemLoader.getIcon();
        if (menuItemLoaderIcon != null) {
            try {
                menuItemLoaderIcon.mutate();
//                menuItemLoaderIcon.setColorFilter(mToolbarWidgetColor, PorterDuff.Mode.SRC_ATOP);
                menuItemLoader.setIcon(menuItemLoaderIcon);
            } catch (IllegalStateException e) {
                Log.i(TAG, "onCreateOptionsMenu: ",e);
            }
            ((Animatable) menuItemLoader.getIcon()).start();
        }
        return super.onCreateOptionsMenu(menu);
    }

 @Override
    public boolean onPrepareOptionsMenu(Menu menu) {

            menu.findItem(R.id.menu_loader).setVisible(mShowProgressMenuItemLoader);
            menu.findItem(R.id.item_action_favorite_add).setVisible(!mShowProgressMenuItemLoader);

        return super.onPrepareOptionsMenu(menu);
    }


@Override
    public void showProgressMenuItem() {
        mShowProgressMenuItemLoader = true;
        invalidateOptionsMenu();
    }

    @Override
    public void hideProgressMenuItem() {
        mShowProgressMenuItemLoader = false;
        invalidateOptionsMenu();
    }

用法:

showProgressMenuItem();
hideProgressMenuItem();