android之官方下拉重新整理元件SwipeRefreshLayout
SwipeRefreshLayout是谷歌官方Sdk提供的下拉元件,繼承自ViewGroup,在support v4相容包下,但必須把你的support library的版本升級到19.1。先看看效果
SwipeRefreshLayout常用API
1、setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener):設定 手勢滑動監聽器。
2、setProgressBackgroundColor(int colorRes):設定進度圈的背景色。
3、setColorSchemeResources(int… colorResIds):設定進度動畫的顏色。
4、setRefreshing(Boolean refreshing):設定元件的重新整理狀態。
5、setSize(int size):設定進度圈的大小,只有兩個值:DEFAULT、LARGE
接下來通過程式碼來介紹SwipeRefreshLayout的具體用法
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
private static final int REFRESH_COMPLETE = 0X110;
private SwipeRefreshLayout mSwipeLayout;
private ListView mListView;
private ArrayAdapter<String> mAdapter;
private List<String> mDatas = new ArrayList<String>(Arrays.asList("Java", "Android", "swift", "IOS",
"HTML"));
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case REFRESH_COMPLETE:
mDatas.addAll(Arrays.asList("C#" , "C++", "Javascript"));
mAdapter.notifyDataSetChanged();
mSwipeLayout.setRefreshing(false);
break;
}
}
};
@SuppressLint("InlinedApi")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.id_listview);
mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
mSwipeLayout.setOnRefreshListener(this);
//如果不設定預設是黑色的
mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
mSwipeLayout.setSize(SwipeRefreshLayout.LARGE);
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDatas);
mListView.setAdapter(mAdapter);
}
public void onRefresh() {
mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);
}
}
佈局檔案
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/id_swipe_ly"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/id_listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
以上就是SwipeRefreshLayout的具體用法,是不是感覺很簡單!!
下面介紹一個在使用SwipeRefreshLayout會遇到的一個問題
如果ListView是SwipeRefreshLayout唯一的孩子,不會出現任何問題,一切都會執行正常。在某些情況下,不僅有ListView可能還有其他的元素。假設有這樣一個介面:
遇到這種情況時,如果我們向上滾動在ListView,一切都如預期那樣顯示。但如果向下滾動,會馬上執行重新整理過程而listView不會滾動。在這種情況下,可以設定SwipeLayout setEnabled(false)禁止使用重新整理通知,當滾動到Listview中第一個項可見時而再啟用它
具體程式碼如下
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
private static final int REFRESH_COMPLETE = 0X110;
private SwipeRefreshLayout mSwipeLayout;
private ListView mListView;
private ArrayAdapter<String> mAdapter;
private List<String> mDatas = new ArrayList<String>(Arrays.asList("Java", "Android", "swift", "IOS",
"HTML"));
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case REFRESH_COMPLETE:
mDatas.addAll(Arrays.asList("C#", "C++", "Javascript"));
mAdapter.notifyDataSetChanged();
mSwipeLayout.setRefreshing(false);
break;
}
}
};
@SuppressLint("InlinedApi")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.id_listview);
mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
mSwipeLayout.setOnRefreshListener(this);
mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
mSwipeLayout.setSize(SwipeRefreshLayout.LARGE);
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDatas);
mListView.setAdapter(mAdapter);
mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem == 0)
mSwipeLayout.setEnabled(true);
else
mSwipeLayout.setEnabled(false);
}
});
}
public void onRefresh() {
mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);
}
}
xml佈局檔案
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/id_swipe_ly"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="android" />
<ListView
android:id="@+id/id_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
ok!SwipeRefreshLayout介紹完畢!有不足之處希望大家指出,謝謝!