Google自己的下拉刷新组件SwipeRefreshLayout非常nice。和LoadMoreListView结合使用,可以非常容易的实现常用的下拉刷新和上拉加载更多功能。以下是一个简单的Demo。
布局如下:>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.cloay.stunningrefreshloadmoredemo.MainActivity"
tools:ignore="MergeRootFrame" >
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.cloay.stunningrefreshloadmoredemo.widgets.LoadMoreListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.cloay.stunningrefreshloadmoredemo.widgets.LoadMoreListView>
</android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>
|
主要代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
| public class MainActivity extends ActionBarActivity implements OnRefreshListener, OnLoadMoreListener{
private SwipeRefreshLayout swipeLayout;
private LoadMoreListView listView;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeLayout = (SwipeRefreshLayout) this.findViewById(R.id.swipe_refresh);
swipeLayout.setOnRefreshListener(this);
// set style
swipeLayout.setColorScheme(android.R.color.holo_red_light, android.R.color.holo_green_light,
android.R.color.holo_blue_bright, android.R.color.holo_orange_light);
listView = (LoadMoreListView) this.findViewById(R.id.listview);
listView.setOnLoadMoreListener(this);
adapter = new MyAdapter();
listView.setAdapter(adapter);
}
@Override
public void onRefresh() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(3*1000); //sleep 3 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
adapter.count = 15;
adapter.notifyDataSetChanged();
swipeLayout.setRefreshing(false);
listView.setCanLoadMore(adapter.count < 45);
super.onPostExecute(result);
}
}.execute();
}
@Override
public void onLoadMore() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(3*1000); //sleep 3 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
adapter.count += 15;
adapter.notifyDataSetChanged();
listView.setCanLoadMore(adapter.count < 45);
listView.onLoadMoreComplete();
super.onPostExecute(result);
}
}.execute();
}
private class MyAdapter extends BaseAdapter{
public int count = 15;
@Override
public int getCount() {
return count;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(MainActivity.this).inflate(android.R.layout.simple_list_item_1, null);
holder.textV = (TextView) convertView.findViewById(android.R.id.text1);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.textV.setText("This is " + (position + 1) + " line.");
return convertView;
}
private class ViewHolder{
TextView textV;
}
}
}
|
效果还不错,小伙伴可以试试哦!Demo已上传Github: StunningPullRefreshAndLoadMoreDemo