常用的刷新技術(三)——PullToRefreshlibrary
阿新 • • 發佈:2019-01-27
PullToRefreshlibrary 下拉重新整理庫
一、簡介
PullToRefreshlibrary 為我們封裝好了各種自定義View,可以直接在佈局檔案中引用,並且都帶有下拉重新整理和上拉載入的功能,給我們帶來了很多方便,不像SwipeRefresh以及Ultra-PullToRefresh那樣需要自己編寫上拉載入的方法。
二、效果圖
1.下拉重新整理
2.上拉載入
三、使用步驟
1.匯入第三方模組:PullToRefreshlibrary
通過視覺化介面關聯到我們的專案中
點選“同步”
2.佈局檔案
例1:
<com.handmark .pulltorefresh.library.PullToRefreshScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ptr= "http://schemas.android.com/apk/res-auto"
android:id="@+id/pullToRefreshScrollView_hot"
android:layout_width="match_parent"
android:layout_height="match_parent"
ptr:ptrMode= "both"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
>
//根據需要在這裡編寫控制元件
</LinearLayout>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView >
注:必須給PullToRefreshScrollView設定屬性ptr:ptrMode= “both”才能支援上拉載入。
例2:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">
<com.handmark.pulltorefresh.library.PullToRefreshGridView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/pullToRefreshGridView_editorRecommend"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:horizontalSpacing="5dp"
android:verticalSpacing="10dp"
ptr:ptrMode="both">
</com.handmark.pulltorefresh.library.PullToRefreshGridView>
</LinearLayout>
注:將PullToRefreshGridView包裹在LinearLayout中。
例3:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">
<com.handmark.pulltorefresh.library.PullToRefreshListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/pullToRefreshListView_update"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="5dp"
android:verticalSpacing="10dp"
ptr:ptrMode="both">
</com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout>
注:將PullToRefreshListView包裹在LinearLayout中。
3.Java程式碼:設定下拉重新整理和上拉載入的監聽器
PullToRefreshListView、PullToRefreshGridView、PullToRefreshScrollView的設定監聽器的方法都一樣。我們以PullToRefreshScrollView為例:
pullToRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
//下拉重新整理
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
//上拉載入更多
}
});
注:選擇設定監聽器時,系統會提示有兩個監聽器,此時我們選擇帶“2”的監聽器。