PullToRefresh庫的用法(供內部分享)

簡介:

該庫支持使用ScrollView, ListView和RecyclerView三大控件場景下的下拉刷新和上拉加載功能,若是在使用過程當中發現缺陷,歡迎指正。java

一. PullToRefreshView

該控件在佈局中做爲Parent Layout包裹如ScrollView、ListView或RecyclerView使用,只支持下拉刷新功能(相似於官方的SwipeRefreshLayout), 使用流程以下:android

Step1:在佈局文件中做爲父佈局引入

<?xml version="1.0" encoding="utf-8"?>
<com.yalantis.phoenix.PullToRefreshView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/pull_to_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:anim_type_view="sun">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:overScrollMode="never"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_title"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:background="@android:color/holo_red_light"
                android:text="Hello World!" />

        </LinearLayout>
    </ScrollView>
</com.yalantis.phoenix.PullToRefreshView>

自定義屬性:app

<declare-styleable name="RefreshView">
        <attr name="anim_type_view" format="enum">
            <enum name="sun" value="0" />
            <enum name="frame" value="1" />
            <enum name="custom" value="2" />
        </attr>
    </declare-styleable>
  • sun:使用默認的下拉頭部背景;
  • frame:設置AnimationDrawable做爲下拉頭部背景;
  • //須要手動設置背景
        AnimationDrawable mAnimationDrawable = (AnimationDrawable) 
        ResourcesCompat.getDrawable(getResources(), R.drawable.windshield_wiper, null);
    
        mPullToRefreshView.setRefreshFrame(new BaseFrameView(mAnimationDrawable, mPullToRefreshView));
  • custom:設置自定義佈局做爲下拉頭部背景;
  • //須要手動設置背景
        View mTopWidget = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.widget_home, null);
        mPullToRefreshView.addCustomeView(mTopWidget);

Step2:設置監聽下拉刷新動做的回調

mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
    @Override
    public void onRefresh() {
        //處理下拉刷新動做
    }
});

其餘方法:

主動開始下拉刷新動做:ide

mPullToRefresh.setRefreshing(true);

主動結束下拉刷新動做:佈局

mPullToRefresh.setRefreshing(false);

 

二. PullToListView

該控件是基於ListView的包裝類,用於支持其下拉刷新和上拉加載功能,同時支持預加載功能(若是未顯示到最後一項將繼續請求下一頁的數據),使用方式等同於ListView。this

Step1:在佈局文件中引入

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.yalantis.phoenix.PulltoRefreshListView 
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cprl_coupon"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/view_coupon"
        android:layout_below="@id/rl_title_coupon_activity"
        app:listViewLayout="@layout/layout_coupon_list"
        app:progressLayout="@layout/layout_progress"
        app:emptyLayout="@layout/layout_empty"
        app:loadMoreEnd="@layout/layout_more_end"
        app:loadMoreProgress="@layout/layout_more_progress"
        app:loadMoreClick="@layout/layout_more_click"/>
</LinearLayout>

自定義屬性:spa

  • listViewLayout屬性:引入一個ListView佈局
  • progressLayput屬性:引入刷新或加載時的進度條佈局
  • emptyLayout屬性:引入當數據爲空時的文案佈局(默認爲"數據爲空")
  • loadMoreEnd屬性:引入當已經拉到最後一項時底部的提示文案佈局 (默認爲"------懸崖勒馬------")
  • loadMoreProgress屬性:引入當正在加載下一頁時底部的提示文案佈局(默認爲「正在努力加載數據...")
  • loadMoreClick屬性:引入當還有下一頁時,未觸發上拉加載時的提示文案佈局 (默認爲「點擊加載更多」)

Step2:設置適配器

PullToRefreshListView mProListView = (PulltoRefreshListView) this.findViewById(R.id.favorite_pro_listview);
    mProListView.setAdapter(listAdapter);

Step3:設置下拉刷新和上拉加載的監聽回調

//設置下拉刷新的監聽回調
    mProListView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
         @Override
         public void onRefresh() {
             mPresenter.refreshProList();
         }
    });

    //設置上拉加載的監聽回調
    mProListView.setOnLoadMoreListener(new BaseRefreshView.OnLoadMoreListener() {
         @Override
         public void onLoadMore() {//當下一頁還有數據時上拉觸發
             mPresenter.loadMorePro();
         }

         @Override
         public boolean needLoadMore() {//判斷是否已滑到底部
             return !mPresenter.ifProArrivedLast();
         }

         @Override
         public void cancelLoadMore() {//當取消上拉加載時觸發

         }
     });

其餘方法:

  • void setRefreshing(boolean result):主動開始或中止執行下拉刷新動做;
  • void setFinishLoadMore():主動結束上拉加載動做;
  • void setRefreshEnable(boolean result):設置是否支持下拉刷新操做;
  • View setEmptyView(int resId):設置數據爲空時的背景;
  • void setFootetView(View footerView):設置底部顯示佈局

 

三. PullToRecyclerView

該控件是基於RecyclerView的包裝類,用於支持其下拉刷新和上拉加載功能,使用方式等同於RecyclerView。code

Step1:在佈局文件中引入

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/c_F0">

    <com.yalantis.phoenix.PulltoRefreshRecyclerView 
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/recycler_store_coupon_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/dp_8"
        android:paddingLeft="@dimen/dp_12"
        android:paddingRight="@dimen/dp_12"
        android:paddingTop="@dimen/dp_8"
        app:listViewLayout="@layout/layout_coupon_list"
        app:progressLayout="@layout/layout_progress"
        app:emptyLayout="@layout/layout_empty"
        app:loadMoreEnd="@layout/layout_more_end"
        app:loadMoreProgress="@layout/layout_more_progress"
        app:loadMoreClick="@layout/layout_more_click"/>
</LinearLayout>

Step2:設置適配器

PulltoRefreshRecyclerView mStoreCouponList = (PulltoRefreshRecyclerView) findViewById(R.id.recycler_store_coupon_list);
    mStoreCouponList.getRecyclerView().setHasFixedSize(false);
    mStoreCouponList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    mStoreCouponList.addItemDecoration(new DividerLayoutDecoration(this, LinearLayoutManager.VERTICAL));
    mStoreCouponList.setAdapter(new RecyclerView.Adapter())

設置監聽流程和具體方法與PullToRefreshListView相同orm

相關文章
相關標籤/搜索