官方連結:https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html#inhfields
情景:前天小海告訴我說Google除了自己的重新整理控制項,沒錯就是SwipeRefreshLayout。
SwipeRefreshLayout字面意思就是下拉重新整理的布局,繼承自ViewGroup,在support v4相容包下(android.support.v4.widget.SwipeRefreshLayout),但必須把你的support library的版本升級到19.1。 提到下拉重新整理大家一定對ActionBarPullToRefresh比較熟悉,而如今google推出了更官方的下拉重新整理組件,這無疑是對開發人員來說比較好的訊息。利用這個組件可以很方便的實現Google Now的重新整理效果,見:
xml布局檔案
This layout should be made the parent of the view that will be refreshed as a result of the gesture and can only support one direct child.
只要在需要重新整理的控制項最外層加上SwipeRefreshLayout,然後他的child首先是可滾動的view,如ScrollView或者ListView。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:ignore="MergeRootFrame" > <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipe_container" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </android.support.v4.widget.SwipeRefreshLayout></FrameLayout>
Activity代碼:
package com.jabony.swiperefreshlayou;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.support.v4.widget.SwipeRefreshLayout;import android.widget.ListView;import com.jabony.swiperefreshlayout.R;public class SwipRefreshLayoutActivity extends Activity implementsSwipeRefreshLayout.OnRefreshListener {private SwipeRefreshLayout swipeLayout;private ListView listView;private ListViewAdapter adapter;private ArrayList<SoftwareClassificationInfo> list;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.swipe_refresh_layout);swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);swipeLayout.setOnRefreshListener(this);//載入顏色是迴圈播放的,只要沒有完成重新整理就會一直迴圈,color1>color2>color3>color4swipeLayout.setColorScheme(android.R.color.white,android.R.color.holo_green_light,android.R.color.holo_orange_light, android.R.color.holo_red_light);list = new ArrayList<SoftwareClassificationInfo>();list.add(new SoftwareClassificationInfo(1, "asdas"));listView = (ListView) findViewById(R.id.list);adapter = new ListViewAdapter(this, list);listView.setAdapter(adapter);}public void onRefresh() {new Handler().postDelayed(new Runnable() {public void run() {swipeLayout.setRefreshing(false);list.add(new SoftwareClassificationInfo(2, "ass"));adapter.notifyDataSetChanged();}}, 3000);}}
主要方法:
- setOnRefreshListener(OnRefreshListener): 為布局添加一個Listener
- setRefreshing(boolean): 顯示或隱藏重新整理進度條
- isRefreshing(): 檢查是否處於重新整理狀態
- setColorScheme(): 設定進度條的顏色主題,最多能設定四種
總結:每次當有朋友對我說有新的事物時,都非常開心,因為這又是一個進步的機會,希望大家與我一同進步。Google也在不斷完善自己的SDK,那麼我們還有什麼理由不跟進時代學習新的設計呢,快清明了 祝大家節日愉快,最好不加班哦!
附件:稍後附上(免積分)