Google提供了一個官方的下拉刷新控件SwipeRefreshLayout,個人感覺還不錯!見慣了傳統的下拉刷新,這個反而給人耳目一新的感覺(Gmail郵箱已經使用這種下拉刷新了)。
SwipeRefreshLayout在v4包下,對應的v4 Demo中也有相應的例子。
SwipeRefreshLayout只能有一個直接子View,可能是一個ListView或一個Layout或其他需要刷新的組件。
setOnRefreshListener 用於監聽刷新的動作。SwipeRefreshLayout 下拉,就會有刷新的效果出來,觸發該監聽。
如果需要一個刷新的動畫,setRefreshing(true), 停: setRefreshing(false)
如果要禁用刷新動畫和手勢響應,ssetEnable(false), 恢復setEnable(true)
Google也在官方網站給出了V4的兼容包:
順便看一眼API呗:
和XlistView差不多,還是很方便使用的,大概就這4個常用的方法,下面貼個簡單的例子。
1、布局文件:
[html] view
plaincopy
-
android:layout_width="match_parent"
-
android:layout_height="match_parent" >
-
-
android:id="@+id/id_swipe_ly"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent" >
-
-
android:id="@+id/id_listview"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent" >
-
-
-
-
2、MainActivty:
[java] view
plaincopy
-
/*
-
* Created by Storm Zhang, Mar 31, 2014.
-
*/
-
-
package com.zhy.swiperefreshlayoutdemo;
-
-
import java.util.ArrayList;
-
import java.util.Arrays;
-
import java.util.List;
-
-
import android.annotation.SuppressLint;
-
import android.app.Activity;
-
import android.os.Bundle;
-
import android.os.Handler;
-
import android.support.v4.widget.SwipeRefreshLayout;
-
import android.util.Log;
-
import android.widget.ArrayAdapter;
-
import android.widget.ListView;
-
-
public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener
-
{
-
-
private static final int REFRESH_COMPLETE = 0X110;
-
private SwipeRefreshLayout mSwipeLayout;
-
private ListView mListView;
-
private ArrayAdapter mAdapter;
-
private List mDatas = new ArrayList(Arrays.asList("Java", "Javascript", "C++", "Ruby", "Json",
-
"HTML"));
-
-
private Handler mHandler = new Handler()
-
{
-
public void handleMessage(android.os.Message msg)
-
{
-
switch (msg.what)
-
{
-
case REFRESH_COMPLETE:
-
mDatas.addAll(Arrays.asList("Lucene", "Canvas", "Bitmap"));
-
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.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);
-
mAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, mDatas);
-
mListView.setAdapter(mAdapter);
-
-
}
-
-
public void onRefresh()
-
{
-
// Log.e("xxx", Thread.currentThread().getName());
-
// UI Thread
-
-
mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);
-
-
}
-
}
效果圖:
參考鏈接:http://blog.csdn.net/lmj623565791/article/details/24521483