Github链接:
https://github.com/chrisbanes/Android-PullToRefresh
GUIDE:
https://github.com/chrisbanes/Android-PullToRefresh/wiki/Quick-Start-Guide
1. 设置Listview
layout文件中定义PullToRefreshListView
Xml代码
1. <com.handmark.pulltorefresh.library.PullToRefreshListView
2. android:id="@+id/list_view"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent" />
2. 设置Mode
- PullToRefreshListView mListView = (PullToRefreshListView) findViewById(R.id.list_view);
- mListView.setMode(Mode.BOTH);
可支持的Mode如下:
- Mode.BOTH:同时支持上拉下拉
- Mode.PULL_FROM_START:只支持下拉Pulling Down
- Mode.PULL_FROM_END:只支持上拉Pulling Up
3. 实现监听Listener
如果Mode设置成Mode.BOTH,需要设置监听OnRefreshListener2,并实现onPullDownToRefresh()、onPullUpToRefresh()两个方法。
如果Mode设置成Mode.PULL_FROM_START或Mode.PULL_FROM_END,需要设置监听OnRefreshListener,同时实现onRefresh()方法。当然也可以设置为OnRefreshListener2,但是Mode.PULL_FROM_START的时候只调用onPullDownToRefresh()方法,Mode.PULL_FROM的时候只调用onPullUpToRefresh()方法。
4. 具体代码如下:
1. public class MainActivity extends
2.
3. private
4. private
5. private
6.
7. @Override
8. protected void
9. super.onCreate(savedInstanceState);
10. setContentView(R.layout.sample);
11.
12. // 设置初期数据
13. new
14. for (int i = 1; i <= 10; i++) {
15. "Item "
16. }
17.
18. // 设置ListView
19. mListView = (PullToRefreshListView) findViewById(R.id.list_view);
20. new
21. mListView.setAdapter(mAdapter);
22.
23. // 设置PullToRefresh
24. mListView.setMode(Mode.BOTH);
25. new
26.
27. // 下拉Pulling Down
28. @Override
29. public void
30. // 下拉的时候数据重置
31. new
32. for (int i = 1; i <= 10; i++) {
33. "Item "
34. }
35. mAdapter.notifyDataSetChanged();
36.
37. new
38. }
39.
40. // 上拉Pulling Up
41. @Override
42. public void
43. // 上拉的时候添加选项
44. int
45. "Item "
46. mAdapter.notifyDataSetChanged();
47.
48. new
49. }
50.
51. });
52.
53. }
54.
55. private class SampleListAdapter extends
56.
57. @Override
58. public int
59. return
60. }
61.
62. @Override
63. public Object getItem(int
64. return
65. }
66.
67. @Override
68. public long getItemId(int
69. return
70. }
71.
72. @Override
73. public View getView(int
74. if(view == null){
75. LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
76. null);
77. }
78. TextView textView = (TextView)view.findViewById(R.id.listItemText);
79. textView.setText(mListItems.get(index));
80. return
81. }
82. }
83.
84. private class FinishRefresh extends
85. @Override
86. protected
87. return null;
88. }
89.
90. @Override
91. protected void
92. mListView.onRefreshComplete();
93. }
94. }
95. }