1. 程式人生 > >一個很強大的下拉重新整理控制元件

一個很強大的下拉重新整理控制元件

Ultra Pull To Refresh

這是現在已經停止維護的下拉重新整理專案的替代方案。繼承於ViewGroup可以包含任何View。功能比SwipeRefreshLayout強大。

使用起來非常簡單。良好的設計,如果你想定製自己的UI樣式,非常簡單,就像給ListView加一個Header View那麼簡單。

支援 API LEVEL >= 8

APK下載

  • 支援所有的View:

    ListView, GridView, ScrollView, FrameLayout, 甚至 TextView.

  • 支援各種下拉重新整理互動.

    • 下拉重新整理(iOS風格)

    • 釋放重新整理(經典風格)

    • 重新整理時,頭部保持(新浪微博)

    • 重新整理時,頭部不保持(微信朋友圈)

    • 自動重新整理,進入介面時自動重新整理

Usage

Maven

<dependency>
    <groupId>in.srain.cube</groupId>
    <artifactId>ultra-ptr</artifactId>
    <type>apklib</type>
    <version>1.0.2</version>
</dependency>

Config

有6個引數可配置:

  • 阻尼係數

    預設: 1.7f,越大,感覺下拉時越吃力。

  • 觸發重新整理時移動的位置比例

    預設,1.2f,移動達到頭部高度1.2倍時可觸發重新整理操作。

  • 回彈延時

    預設 200ms,回彈到重新整理高度所用時間

  • 頭部回彈時間

    預設1000ms

  • 重新整理是保持頭部

    預設值 true.

  • 下拉重新整理 / 釋放重新整理

    預設為釋放重新整理

xml中配置示例
<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>

也可以在java程式碼中配置

// 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);

StoreHouse 風格

  • 使用字串, 支援A-Z, 0-7 以及 - .
// header
final StoreHouseHeader header = new StoreHouseHeader(getContext());
header.setPadding(0, LocalDisplay.dp2px(15), 0, 0);

/**
 * using a string, support: A-Z 0-9 - .
 * you can add more letters by {@link in.srain.cube.views.ptr.header.StoreHousePath#addChar}
 */
header.initWithString('Alibaba');
  • 使用資原始檔中的路徑資訊
header.initWithStringArray(R.array.storehouse);

資原始檔 res/values/arrays.xml 內容如下:

<resources>
    <string-array name="storehouse">
        <item>0,35,12,42,</item>
        <item>12,42,24,35,</item>
        <item>24,35,12,28,</item>
        <item>0,35,12,28,</item>
        <item>0,21,12,28,</item>
        <item>12,28,24,21,</item>
        <item>24,35,24,21,</item>
        <item>24,21,12,14,</item>
        <item>0,21,12,14,</item>
        <item>0,21,0,7,</item>
        <item>12,14,0,7,</item>
        <item>12,14,24,7,</item>
        <item>24,7,12,0,</item>
        <item>0,7,12,0,</item>
    </string-array>
</resources>

處理重新整理

通過PtrHandler,可以檢查確定是否可以下來重新整理以及在合適的時間重新整理資料。

檢查是否可以下拉重新整理在PtrDefaultHandler.checkContentCanBePulledDown中有預設簡單的實現,你可以根據實際情況完成這個邏輯。

public interface PtrHandler {

    /**
     * 檢查是否可以執行下來重新整理,比如列表為空或者列表第一項在最上面時。
     * <p/>
     * {@link in.srain.cube.views.ptr.PtrDefaultHandler#checkContentCanBePulledDown}
     */
    public boolean checkCanDoRefresh(final PtrFrameLayout frame, final View content, final View header);

    /**
     * 需要載入資料時觸發
     *
     * @param frame
     */
    public void onRefreshBegin(final PtrFrameLayout frame);
}

例子:

ptrFrame.setPtrHandler(new PtrHandler() {
    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        frame.postDelayed(new Runnable() {
            @Override
            public void run() {
                ptrFrame.refreshComplete();
            }
        }, 1800);
    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        // 預設實現,根據實際情況做改動
        return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
    }
});

License

Apache 2

Contact & Help

Please fell free to contact me if there is any problem when using the library.