效果图:
MainActivity.java<span style="font-family:Arial;">package com.hnpolice.luoxiaoke.swiperefreshlayout;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.InjectView;
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
@InjectView(R.id.list_view)
ListView listView;
@InjectView(R.id.refresh_layout)
SwipeRefreshLayout refreshLayout;
//下拉刷新状态
private static final int REFRESH_COMPLETE = 110;
private List<String> datas = new ArrayList<>();
private ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
// Set the listener to be notified when a refresh is triggered via the swipe gesture.
refreshLayout.setOnRefreshListener(this);
// Set the colors used in the progress animation.
refreshLayout.setColorSchemeResources(R.color.color_blue, R.color.color_green, R.color.color_red);
for (int i = 0; i < 5; i++) {
datas.add("item" + i);
}
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, datas);
listView.setAdapter(adapter);
}
@Override
public void onRefresh() {
//2000 : 2s后执行
mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);
}
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case REFRESH_COMPLETE:
datas.add("added");
adapter.notifyDataSetChanged();
refreshLayout.setRefreshing(false);
break;
}
}
};
}</span><span style="font-family:Consolas;">
</span>activity_main.xml<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
colors.xml<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="color_red">#ff0000</color>
<color name="color_green">#00ff00</color>
<color name="color_blue">#0000ff</color>
</resources>
工程下载地址:http://download.csdn.net/detail/lxk_1993/9375711