Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android PullToRefresh上拉和下拉刷新

Android PullToRefresh上拉和下拉刷新

編輯:關於Android編程

Github鏈接:

https://github.com/chrisbanes/Android-PullToRefresh

1. 設置Listview
layout文件中定義PullToRefreshListView

Xml代碼

  1. android:id="@+id/list_view"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent" />
    2. 設置Mode
    1. PullToRefreshListView mListView = (PullToRefreshListView) findViewById(R.id.list_view);
    2. 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 Activity {
        2. private ArrayList mListItems;
        3. private PullToRefreshListView mListView;
        4. private SampleListAdapter mAdapter;
        5. @Override
        6. protected void onCreate(Bundle savedInstanceState) {
        7. super.onCreate(savedInstanceState);
        8. setContentView(R.layout.sample);
        9. // 設置初期數據
        10. mListItems = new ArrayList();
        11. for (int i = 1; i <= 10; i++) {
        12. mListItems.add("Item " + Integer.toString(i));
        13. }
        14. // 設置ListView
        15. mListView = (PullToRefreshListView) findViewById(R.id.list_view);
        16. mAdapter = new SampleListAdapter();
        17. mListView.setAdapter(mAdapter);
        18. // 設置PullToRefresh
        19. mListView.setMode(Mode.BOTH);
        20. mListView.setOnRefreshListener(new OnRefreshListener2(){
        21. // 下拉Pulling Down
        22. @Override
        23. public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        24. // 下拉的時候數據重置
        25. mListItems = new ArrayList();
        26. for (int i = 1; i <= 10; i++) {
        27. mListItems.add("Item " + Integer.toString(i));
        28. }
        29. mAdapter.notifyDataSetChanged();
        30. new FinishRefresh().execute();
        31. }
        32. // 上拉Pulling Up
        33. @Override
        34. public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        35. // 上拉的時候添加選項
        36. int count = mListItems.size();
        37. mListItems.add("Item " + Integer.toString(++count));
        38. mAdapter.notifyDataSetChanged();
        39. new FinishRefresh().execute();
        40. }
        41. });
        42. }
        43. private class SampleListAdapter extends BaseAdapter {
        44. @Override
        45. public int getCount() {
        46. return mListItems.size();
        47. }
        48. @Override
        49. public Object getItem(int index) {
        50. return mListItems.get(index);
        51. }
        52. @Override
        53. public long getItemId(int index) {
        54. return index;
        55. }
        56. @Override
        57. public View getView(int index, View view, ViewGroup arg2) {
        58. if(view == null){
        59. LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        60. view = inflater.inflate(R.layout.list_item, null);
        61. }
        62. TextView textView = (TextView)view.findViewById(R.id.listItemText);
        63. textView.setText(mListItems.get(index));
        64. return view;
        65. }
        66. }
        67. private class FinishRefresh extends AsyncTask{
        68. @Override
        69. protected Void doInBackground(Void... params) {
        70. return null;
        71. }
        72. @Override
        73. protected void onPostExecute(Void result){
        74. mListView.onRefreshComplete();
        75. }
        76. }
        77. }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved