https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html
效果是这样的:
上个代码吧,使用比较简单:
package com.ali.yunos.androiddemo_n.activities;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.ali.yunos.androiddemo_n.R;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class SwipeRefreshActivity extends Activity {
private SwipeRefreshLayout mSwipeLayout;
private ListView mLV;
private List<String> mData;
private ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe_refresh);
mSwipeLayout = (SwipeRefreshLayout)findViewById(R.id.sample_swipe);
mLV = (ListView)findViewById(R.id.swipe_listview);
initData();
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mData);
mLV.setAdapter(mAdapter);
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.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mData.add(0, "添加新的item" + new Random().nextInt());
mAdapter.notifyDataSetChanged();
//停止刷新动画
mSwipeLayout.setRefreshing(false);
}
}, 3000);
}
});
}
private void initData() {
mData = new ArrayList<String>();
for (int i = 0; i < 50; i++) {
mData.add("Item#" + i);
}
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sample_swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/swipe_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
注意 SwipeRefreshLayout套在ListView外面,目前支持ListView和GridView。
To add the swipe to refresh widget to an existing app, add SwipeRefreshLayout
as the parent of a single ListView
or GridView
. Remember that SwipeRefreshLayout
only supports a single ListView
or GridView
child.
然后监听swipe动作做出响应。