在最新的 Android Support Library, revision 19.1.0 (March 2014) 添加了SwipeRefreshLayout控件。
版本发布的说明信息如下:
Changes for v4 support library:
Added the SwipeRefreshLayout class, which enables users to refresh the contents of a view with a vertical swipe gesture.
Fixed accessibility issues with navigation drawers. Changes for v7 appcompat library:
Fixed background issues with the action bar.
链接:http://developer.android.com/intl/zh-cn/tools/support-library/index.html
SwipeRefreshLayout 的官方说明地址如下:
http://developer.android.com/intl/zh-cn/reference/android/support/v4/widget/SwipeRefreshLayout.html
几点重要的说明如下:
1. SwipeRefreshLayout 是用来做下拉刷新的,可以实现Google Now上的下拉刷新效果
2. SwipeRefreshLayout 只能有一个直接的子view,也就是说SwipeRefreshLayout 只能直接包裹一个子view(当然这个子view可以包含多个view), 如果不满足这个条件,运行的时候会报如下的错误: java.lang.IllegalStateException: SwipeRefreshLayout can host only one direct child
3. SwipeRefreshLayout 包裹的子view必须是可以支持下拉刷新手势的,例如ListView和ScrollView 。
使用的demo 如下 :
效果图:
核心的代码 :
package com.hsx.demo; import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView; public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {
private SwipeRefreshLayout swipeLayout;
List<String> data = new ArrayList<String>();
MyAdapter adapter; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData(); 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);
ListView listView = (ListView) findViewById(R.id.test_listview);
adapter = new MyAdapter();
listView.setAdapter(adapter);
} private void initData() {
data.add("苏州");
data.add("北京");
data.add("上海");
} Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
swipeLayout.setRefreshing(false);
adapter.notifyDataSetChanged();
} }; public void onRefresh() { new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Random random = new Random();
data.add("天津" + random.nextInt(100));
handler.sendEmptyMessage(1000);
}
}).start();
} private class MyAdapter extends BaseAdapter { @Override
public int getCount() { return data.size();
} @Override
public Object getItem(int position) { return data.get(position);
} @Override
public long getItemId(int position) { return 0;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item, null);
TextView tvItem = (TextView) convertView.findViewById(R.id.item_tv);
tvItem.setText(data.get(position));
return convertView;
}
} }
主布局文件 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ListView
android:id="@+id/test_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white" >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout> </LinearLayout>
但是该控件目前感觉尚不完整,无法真正的使用到项目中。例如这样的一个问题,可以再下拉刷新的过程中继续下拉刷新,目前还没有想到好的办法解决 。期待后续的Google优化。
demo下载地址:http://download.csdn.net/detail/abc13939746593/7131001