Tag: actionbar
Note: You need to import the V4 package.
Private menuitem refreshitem;
@ Override
Public Boolean onoptionsitemselected (menuitem item ){
Switch (item. getitemid ()){
Case R. Id. action_refresh:
Showrefreshanimation (item );
Return true;
Default:
Return super. onoptionsitemselected (item );
}
}
Private void showrefreshanimation (menuitem item ){
Refreshitem = item;
// Use an imageview to set it to the actionview of menuitem, so that we can use this imageview to display the rotation animation.
Imageview refreshactionview = (imageview) layoutinflater. From (getactivity ()).
Inflate (R. layout. actionbar_refresh_view, null );
Refreshactionview. setimageresource (R. drawable. refreshnormal );
// Refreshitem. setactionview (refreshactionview); // This method must be later than 11
Menuitemcompat. setactionview (item, refreshactionview); // replace it with the V4 compatibility package.
// Display the refresh Animation
Animation animation = animationutils. loadanimation (getactivity (), R. anim. rotateanim );
Animation. setrepeatmode (animation. Restart );
Animation. setrepeatcount (animation. Infinite );
Refreshactionview. startanimation (animation );
}
Public void hiderefreshanimation (){
If (refreshitem! = NULL ){
/// View = refreshitem. getactionview ();
View view = menuitemcompat. getactionview (refreshitem );
If (view! = NULL ){
View. clearanimation ();
Refreshitem. setactionview (null );
}
}
}
<Menu xmlns: Android = "http://schemas.android.com/apk/res/android"
Xmlns: funshion = "http://schemas.android.com/apk/res-auto">
<Item
Android: Id = "@ + ID/action_deleted"
Android: icon = "@ drawable/common_delete_btn_selector"
Android: Title = "@ string/Delete"
Funshion: showasaction = "ifroom"/>
<Item
Android: Id = "@ + ID/action_refresh"
Android: icon = "@ drawable/refreshnormal"
Android: Title = "@ string/refresh"
Funshion: showasaction = "ifroom"/>
</Menu>
Actionbar_refresh_view.xml
<Imageview xmlns: Android = "http://schemas.android.com/apk/res/android"
Style = "@ Android: style/widget. actionbutton"
Android: layout_height = "wrap_content"
Android: scaletype = "centerinside"/>
How to add an animation to the actionbar (2.3 supported)