1. 程式人生 > >android-Ultra-Pull-To-Refresh 使用簡介

android-Ultra-Pull-To-Refresh 使用簡介

專案地址https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh

該專案只包含下拉重新整理,可以包裹任何控制元件,如果需要新增上拉載入,可以將包裹的view改為前篇文章的LoadMoreRecyclerView實現上拉載入功能。

引入方法1:直接匯入lib庫:ptr-lib,可到專案地址下載,然後將lib新增到專案

compile project(':ptr-lib')
方法2:中央庫依賴compile 'in.srain.cube:ultra-ptr:1.0.11'
配置
有6個引數可配置:
  • 阻尼係數預設: 1.7f,越大,感覺下拉時越吃力。
  • 觸發重新整理時移動的位置比例預設,1.2f,移動達到頭部高度1.2倍時可觸發重新整理操作。
  • 回彈延時預設 200ms,回彈到重新整理高度所用時間
  • 頭部回彈時間預設1000ms
  • 重新整理是保持頭部預設值 true.
  • 下拉重新整理 / 釋放重新整理預設為釋放重新整理
<in.srain.cube.views.ptr.PtrFrameLayout
    android:id="@+id/store_house_ptr_frame"
    xmlns:cube_ptr="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 
    cube_ptr:ptr_resistance="1.7"
    cube_ptr:ptr_ratio_of_header_height_to_refresh="1.2"
    cube_ptr:ptr_duration_to_close="300"
    cube_ptr:ptr_duration_to_close_header="2000"
    cube_ptr:ptr_keep_header_when_refresh="true"
    cube_ptr:ptr_pull_to_fresh="false" >
 
    <LinearLayout
        android:id="@+id/store_house_ptr_image_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/cube_mints_333333"
        android:clickable="true"
        android:padding="10dp">
 
        <in.srain.cube.image.CubeImageView
            android:id="@+id/store_house_ptr_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
 
</in.srain.cube.views.ptr.PtrFrameLayout>
也可以在程式碼中配置
// the following are default settings
mPtrFrame.setResistance(1.7f);
mPtrFrame.setRatioOfHeaderHeightToRefresh(1.2f);
mPtrFrame.setDurationToClose(200);
mPtrFrame.setDurationToCloseHeader(1000);
// default is false
mPtrFrame.setPullToRefresh(false);
// default is true
mPtrFrame.setKeepHeaderWhenRefresh(true);
重新整理時,保持內容不動,僅頭部下移, setPinContent()

下拉重新整理功能介面,對下拉重新整理功能的抽象,包含以下兩個方法。

public void onRefreshBegin(final PtrFrameLayout frame)

重新整理回撥函式,使用者在這裡寫自己的重新整理功能實現,處理業務資料的重新整理。

public boolean checkCanDoRefresh(final PtrFrameLayout frame, final View content, final View header)

判斷是否可以下拉重新整理。 UltraPTR 的 Content 可以包含任何內容,使用者在這裡判斷決定是否可以下拉。
例如,如果 Content 是 TextView,則可以直接返回 true,表示可以下拉重新整理。
如果 Content 是 ListView,當第一條在頂部時返回 true,表示可以下拉重新整理。
如果 Content 是 ScrollView,當滑動到頂部時返回 true,表示可以重新整理。

案例示例:此段程式碼在設定了rvView(RecycleView)相關配置之後之後進行設定即可,
 mPtrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
               // currentPage = 1;
              //  mPresenter.start(currentPage,type);
                mPtrFrame.refreshComplete();
                if(currentPage==1){
                    mDataList.clear();
                }
                mDataList.addAll(entity.getNewslist());
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }
        });
        mPtrFrame.setResistance(1.7f);
        mPtrFrame.setRatioOfHeaderHeightToRefresh(1.2f);
        mPtrFrame.setDurationToClose(200);
        mPtrFrame.setDurationToCloseHeader(1000);
        // default is false
        mPtrFrame.setPullToRefresh(false);
        // default is true
        mPtrFrame.setKeepHeaderWhenRefresh(true);
        mPtrFrame.postDelayed(new Runnable() {
            @Override
            public void run() {
                mPtrFrame.autoRefresh();
            }
        }, 100);