標籤:android http color os 檔案 width
今天在Google+上看到了SwipeRefreshLayout這個名詞,遂搜尋了下,發現竟然是剛剛google更新sdk新增加的一個widget,於是趕緊搶先體驗學習下。
SwipeRefreshLayout
SwipeRefreshLayout字面意思就是下拉重新整理的布局,繼承自ViewGroup,在support v4相容包下,但必須把你的support library的版本升級到19.1。 提到下拉重新整理大家一定對ActionBarPullToRefresh比較熟悉,而如今google推出了更官方的下拉重新整理組件,這無疑是對開發人員來說比較好的訊息。利用這個組件可以很方便的實現Google Now的重新整理效果,見:
主要方法
setOnRefreshListener(OnRefreshListener): 為布局添加一個Listener
setRefreshing(boolean): 顯示或隱藏重新整理進度條
isRefreshing(): 檢查是否處於重新整理狀態
setColorScheme(): 設定進度條的顏色主題,最多能設定四種
xml布局檔案
布局檔案很簡單,只需要在最外層加上SwipeRefreshLayout,然後他的child是可滾動的view即可,如ScrollView或者ListView。如:
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swipe_container" android:layout_width="match_parent" android:layout_height="match_parent"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="@string/hello_world" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:gravity="center"/> </ScrollView> </android.support.v4.widget.SwipeRefreshLayout>
Activity代碼
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container); swipeLayout.setOnRefreshListener(this); swipeLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);} public void onRefresh() { new Handler().postDelayed(new Runnable() { @Override public void run() { swipeLayout.setRefreshing(false); } }, 5000);}
上面的代碼很簡單,只需要給SwipeRefreshLayout添加一個listener,值得說明的是setColorScheme方法是設定重新整理進度條的顏色,最多隻能設定4種迴圈顯示,預設第一個是隨使用者手勢載入的顏色進度條。
源碼
寫了的小demo在github上,地址在:SwipeRefreshLayoutDemo
總結
google在不斷完善自己的sdk,推出越來越多的組件,其目的是讓開發更簡單,設計上更統一,這可能是google未來的方向,不管怎樣,這對開發人員來說無疑是非常好的訊息。