應用場景:在界面内清單或其他部件下拉重新整理時,actionbar 出現一個轉圈的重新整理标示動畫。
實作方式:可使用開源類庫 refreshactionitem (https://github.com/manuelpeinado/refreshactionitem),refreshactionitem 還支援一些擴充功能,功能比較豐富;
如果隻需要實作一個重新整理和loading的效果,則可以使用另一種簡便的實作方式:
1. 首先定義一個 menu xml 檔案, share_public.xml:
[html] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/refresh_loading"
android:icon="@color/transparent"
android:showasaction="always"
android:title="重新整理"/>
</menu>
2. 然後建立一個代表重新整理進度的自定義 progressbar 布局檔案 actionbar_indeterminate_progress.xml:
<framelayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="56dp"
android:minwidth="56dp">
<progressbar android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center"
style="?indeterminateprogressstyle" />
</framelayout>
注意,為了顯示美觀,上面的 寬度和高度 不同的版本和螢幕可能需要設定不一樣的值,可以在不同的 dimens.xml 中設定。
3. 在 activity 代碼中,擷取到該 menuitem 并根據重新整理情況來設定 actionview:
[java] view
menuitem mprogressmenu;
@override
public boolean oncreateoptionsmenu(menu menu) {
getsupportmenuinflater().inflate(r.menu.share_public, menu);
mprogressmenu = menu.finditem(r.id.refresh_loading);
return true;
}
public void setloadingstate(boolean refreshing) {
if (mprogressmenu != null) {
if (refreshing) {
mprogressmenu
.setactionview(r.layout.actionbar_indeterminate_progress);
mprogressmenu.setvisible(true);
} else {
mprogressmenu.setvisible(false);
mprogressmenu.setactionview(null);
}
}
現在,根據您的重新整理邏輯,隻需要調用 setrefreshactionbuttonstate 函數就可以啟用重新整理動畫了。
本項目不在 actionview 中處理 onclick 事件,使用者點選該菜單是沒響應的,采用此種方式,loading狀态動畫隻會在重新整理時進行,在重新整理結束後隐藏。
注:本帖系在參考位址基礎上改造,本項目使用了actionbarsherlock來相容3.0以下的版本中的actionbar;
參考位址使用了appcompat作相容,在處理添加actionview操作上略有不同。
參考位址read more: http://blog.chengyunfeng.com/?p=572