直接上代码:
<?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/id_swipe_ly"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
public class MainActivity extends Activity 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("aaaaa", "bbbbb", "ccccc", "ddddd"));
private Handler mHandler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
switch (msg.what)
{
case REFRESH_COMPLETE:
mDatas.addAll(Arrays.asList("eee", "fff", "gggg"));
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);
<span style="white-space:pre"> </span>mSwipeLayout.setColorSchemeResources(R.color.aliceblue,
<span style="white-space:pre"> </span>R.color.black,
<span style="white-space:pre"> </span>R.color.gold,
<span style="white-space:pre"> </span>R.color.red);
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDatas);
mListView.setAdapter(mAdapter);
}
public void onRefresh()
{
mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);
}
}