I found the solution by setting the View
also for the non-animated item to be the same. In the menu.xml
:
<item
android:id="@+id/action_refresh"
android:orderInCategory="100"
android:icon="@drawable/ic_menu_refresh"
android:actionLayout="@layout/refresh_action_view"
android:showAsAction="ifRoom"
style="@android:style/Widget.ActionButton"
android:title="@string/action_refresh"/>
Then, in onCreateOptionsMenu
, I fetch the view in a member variable, and attach an onclick handler:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.orders, menu);
final Menu m = menu;
refreshItem = menu.findItem(R.id.action_refresh);
refreshItem.getActionView().setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
m.performIdentifierAction(refreshItem.getItemId(), 0);
}
});
return true;
}
In onCreate
, we load the animation:
rotation = AnimationUtils.loadAnimation(this, R.anim.clockwise_refresh);
rotation.setRepeatCount(Animation.INFINITE);
Finally, call this to start the animation:
refreshItem.getActionView().startAnimation(rotation);
and this to stop it:
refreshItem.getActionView().clearAnimation();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…