首页 > 技术文章 > 简单的下拉刷新以及优化--SwipeRefreshLayout

zzw1994 2015-11-24 12:09 原文

代码工程简要说明:以一个SwipeRefreshLayout包裹ListView,SwipeRefreshLayout接管ListView的下拉事件,若ListView被用户触发下拉动作后,SwipeRefreshLayout启动下拉刷新的UI表现样式,下拉刷新完毕,在SwipeRefreshLayout提供的接口中回调更新ListView中的数据。

activity_main.xml:

 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中操作:

actuvity_main.xml不变,变化的是MainActivity.java:

代码为:

 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 }

最后的结果如下图:

推荐阅读