下面App基本都有下拉重新整理的功能,以前基本都使用XListView或者自己寫一個下拉重新整理,近期Google提供了一個官方的下拉重新整理控制項 SwipeRefreshLayout,我感覺還不錯啊,見慣了傳統的下拉重新整理,這個反而給人耳目一新的感覺(貌似知乎的APP已經使用這種下拉重新整理了)。
Google也在官方網站給出了V4的相容包:
再來看看布局檔案裡的代碼(我這裡放的是一個ListView 當然也可以放其他控制項 只要你高興就好)
<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/main_srl_bloglist" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/main_lv_bolg_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:divider="#00000000" android:dividerHeight="5dp"></ListView> </android.support.v4.widget.SwipeRefreshLayout>
最後來看看Activiy裡的代碼吧(這裡我用的AndroidAnnotations 所以沒有寫 findViewById 哈哈)
//設定重新整理時動畫的顏色,可以設定4個 mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light); mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { mIndex=1; mBlogList.clear(); loadBlogData(); } });
在onRefresh 裡調用擷取資料的方法就好了 資料擷取完畢 別忘了 修改狀態
mSwipeRefreshLayout.setRefreshing(false);
哈哈 以前要寫成噸的代碼 用SwipeRefreshLayout 就這幾行代碼 是不是很方便。