9

我尝试以类似于用于 Honeycomb 的 ActionBarHelper的方式使用操作视图为 ActionBar 设置 MenuItem 动画以具有旋转刷新按钮。但我有两个麻烦:

  1. 当我显示动作视图时,项目的宽度小于 MenuItem(请参阅屏幕截图的第 1 部分和第 2 部分)
  2. 当我想通过 setActionView(null) 停止动画并返回默认 MenuItem 状态时,我的操作视图仍显示在我的 MenuItem 内(屏幕截图的第 3 部分)

MenuItem的不同步骤(截图)

ActionView 布局 (R.layout.menu_item_refresh) 是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_menu_refresh" />

</LinearLayout>

onOptionsItemSelected 方法代码是:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){               
        default:
            return super.onOptionsItemSelected(item);

        case R.id.Menu_Feed_Refresh:
            mAnimatedItem = item;
            item.setActionView(R.layout.menu_item_refresh);
            item.startAnimation(mAnimation);

            load();
            return true;
    }
}

加载完成后,我调用处理程序 mAnimatedItem.setActionView(null)

4

2 回答 2

0

mAnimation.clearAnimation()当你想停下来的时候打电话。

于 2013-07-29T17:46:58.153 回答
0

如果我正确理解了您的代码,则将动画应用于 MenuItem。您删除 ActionView setActionView(null),但 menuItem 动画。

您需要从 ActionView 获取图像并对其应用动画:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){               
    default:
        return super.onOptionsItemSelected(item);

    case R.id.Menu_Feed_Refresh:
        mAnimatedItem = item;
        item.setActionView(R.layout.menu_item_refresh);
        ImageView iv = (ImageView) item.getActionView().findViewById(R.id.refresh_image_id);
        iv.startAnimation(mAnimation);
        load();
        return true;
    }
}
于 2012-05-14T16:46:51.787 回答