代码工程简要说明:以一个SwipeRefreshLayout包裹ListView,SwipeRefreshLayout接管ListView的下拉事件,若ListView被用户触发下拉动做后,SwipeRefreshLayout启动下拉刷新的UI表现样式,下拉刷新完毕,在SwipeRefreshLayout提供的接口中回调更新ListView中的数据。java
activity_main.xml:android
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="com.zzw.testswiperefreshlayout.MainActivity" > 6 7 <android.support.v4.widget.SwipeRefreshLayout 8 android:id="@+id/swipeRefreshLayoyut" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" > 11 12 <ListView 13 android:id="@+id/listView" 14 android:layout_width="match_parent" 15 android:layout_height="match_parent" /> 16 </android.support.v4.widget.SwipeRefreshLayout> 17 18 </RelativeLayout>
MainActivity.java:网络
1 package com.zzw.testswiperefreshlayout; 2 3 import java.util.ArrayList; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.support.v4.widget.SwipeRefreshLayout; 8 import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener; 9 import android.widget.ArrayAdapter; 10 import android.widget.ListView; 11 12 public class MainActivity extends Activity { 13 14 private SwipeRefreshLayout swipeRefreshLayout; 15 16 private int count = 0; 17 private ArrayList<String> data; 18 private ArrayAdapter<String> adapter; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 25 data = new ArrayList<String>(); 26 27 swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayoyut); 28 ListView listView = (ListView) findViewById(R.id.listView); 29 30 // 设置刷新动画的颜色,能够设置1或者更多. 31 // 咱们暂时使用三个Android系统自带的颜色。 32 swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_red_light, android.R.color.holo_green_light, 33 android.R.color.holo_orange_light); 34 35 swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() { 36 37 @Override 38 public void onRefresh() { 39 longTimeOperation(); 40 } 41 }); 42 // 使用Android系统自带的一个简单TextView布局文件android.R.layout.simple_list_item_1显示咱们的数据内容。 43 adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data); 44 45 listView.setAdapter(adapter); 46 } 47 48 // 每一次下拉刷新将触发更新操做动做。 49 // 这里将是比较耗时的操做:如网络请求的数据,加载一个大图片。 50 // 简单期间,咱们假设就是简单的将count数据加1,而后更新显示。 51 // 52 // 备注:swipeRefreshLayout.setRefreshing(true) 到 53 // swipeRefreshLayout.setRefreshing(false)之间的这段代码 , 54 // 在实际的应用开发中通常就是线程化的、耗时的或者后台的操做代码。 55 private void longTimeOperation() { 56 // true,刷新开始,因此启动刷新的UI样式. 57 swipeRefreshLayout.setRefreshing(true); 58 59 // 开始启动刷新... 60 // 在这儿放耗时操做的 AsyncTask线程、后台Service等代码。 61 62 // add(0,xxx)每次将更新的数据xxx添加到头部。 63 data.add(0, "" + count++); 64 adapter.notifyDataSetChanged(); 65 66 // 刷新完毕 67 // false,刷新完成,所以中止UI的刷新表现样式。 68 swipeRefreshLayout.setRefreshing(false); 69 } 70 71 }
在上面若是遇到一个耗时操做就会形成主线程堵塞,因此将上述的小Demo进行了简单的优化,把耗时操做放在了一个AsyncTask中操做:app
actuvity_main.xml不变,变化的是MainActivity.java:ide
代码为:布局
1 package com.zzw.testswiperefreshlayout; 2 3 import java.util.ArrayList; 4 5 import android.app.Activity; 6 import android.os.AsyncTask; 7 import android.os.Bundle; 8 import android.os.SystemClock; 9 import android.support.v4.widget.SwipeRefreshLayout; 10 import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener; 11 import android.widget.ArrayAdapter; 12 import android.widget.ListView; 13 14 public class MainActivity extends Activity { 15 16 private SwipeRefreshLayout swipeRefreshLayout; 17 18 private int count = 0; 19 private ArrayList<String> data; 20 private ArrayAdapter<String> adapter; 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_main); 26 27 data = new ArrayList<String>(); 28 29 swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayoyut); 30 ListView listView = (ListView) findViewById(R.id.listView); 31 32 // 设置刷新动画的颜色,能够设置1或者更多. 33 // 咱们暂时使用三个Android系统自带的颜色。 34 swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_red_light, android.R.color.holo_green_light, 35 android.R.color.holo_orange_light); 36 37 swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() { 38 39 @Override 40 public void onRefresh() { 41 new MyAsyncTask().execute(); 42 } 43 }); 44 // 使用Android系统自带的一个简单TextView布局文件android.R.layout.simple_list_item_1显示咱们的数据内容。 45 adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data); 46 47 listView.setAdapter(adapter); 48 } 49 50 private class MyAsyncTask extends AsyncTask { 51 52 // 初始化 53 @Override 54 protected void onPreExecute() { 55 // true,刷新开始,因此启动刷新的UI样式. 56 swipeRefreshLayout.setRefreshing(true); 57 } 58 59 protected Object doInBackground(Object... params) { 60 // 假设耗时5秒 61 SystemClock.sleep(5000); 62 return count++; 63 } 64 65 @Override 66 protected void onPostExecute(Object result) { 67 // add(0,xxx)每次将更新的数据xxx添加到头部。 68 data.add(0, result + ""); 69 adapter.notifyDataSetChanged(); 70 71 // 刷新完毕 72 // false,刷新完成,所以中止UI的刷新表现样式。 73 swipeRefreshLayout.setRefreshing(false); 74 } 75 76 } 77 78 }
最后的结果以下图:优化