1. 程式人生 > >PullToRefresh上下拉重新整理載入(基礎)

PullToRefresh上下拉重新整理載入(基礎)

  • 匯入依賴

  • compile 'com.github.userswlwork:pull-to-refresh:1.0.0'

  • XML程式碼
    <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/pull" android:layout_width="match_parent" android:layout_height="match_parent"></com.handmark.pulltorefresh.library.PullToRefreshListView>

  • MainActivity中的程式碼

ArrayList<Bean.DataBean> list = new ArrayList<Bean.DataBean>();//這是網路介面請求的資料
	private PullToRefreshListView pull;
	pull= view.findViewById(R.id.pull);//找控制元件
	pull.setMode(PullToRefreshListView.Mode.BOTH);//支援上下拉
	/設定監聽
        pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                list.clear();
                initData(0);//這是一個方法
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                page ++;
                initData(page);
            }
        });


pull.onRefreshComplete();//最後結束