Android中 android-Ultra-Pull-To-Refresh庫的簡單使用

最近用到了android-Ultra-Pull-To-Refresh這個庫,感覺功能如實的強大

此庫的地址是https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh  想了解的可以去github上看,介紹還是很詳細的。

我在這裏只簡單介紹一下使用此庫自定義頭部刷新佈局。

1.首先在Module:app中加入

    implementation 'in.srain.cube:ultra-ptr:1.0.11'

2. 在你想刷新的佈局中加入PtrFrameLayout,包裹你要刷新佈局。

例如:

<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>

3. 現在開始自定義頭部佈局,定義一個類集成view(LinearLayout,FrameLayout,等)實現 PtrUIHandler

package com.muxi.ant.ui.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.muxi.ant.R;

import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;

//首頁下拉刷新的頭部
//參考:https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh/blob/master/README-cn.md
//https://blog.csdn.net/shineflowers/article/details/59033536
public class JdRefreshHeader extends FrameLayout implements PtrUIHandler {

    private TextView mTvRemind;
    private int mState;
    private ImageView iv;
    private ProgressBar progress;
    private AnimationDrawable frameAnim;
    /**
     * 重置
     * 準備刷新
     * 開始刷新
     * 結束刷新
     */
    public static final int STATE_RESET = -1;
    public static final int STATE_PREPARE = 0;
    public static final int STATE_BEGIN = 1;
    public static final int STATE_FINISH = 2;

    public JdRefreshHeader(Context context) {
        this(context, null);
    }

    public JdRefreshHeader(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public JdRefreshHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView();
    }

    /**
     * 初始化view
     */
    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.refresh_header_view, this, false);
        mTvRemind = (TextView) view.findViewById(R.id.tv_remain);
        iv = view.findViewById(R.id.iv);
        progress = view.findViewById(R.id.progress);

        // 通過逐幀動畫的資源文件獲得AnimationDrawable示例
        frameAnim = (AnimationDrawable) getResources().getDrawable(R.drawable.fist_loading);
        // 把AnimationDrawable設置爲ImageView的背景
        iv.setBackgroundDrawable(frameAnim);

        addView(view);
    }


    @Override
    public void onUIReset(PtrFrameLayout frame) {
        //重置
        mState = STATE_RESET;
        mTvRemind.setText(R.string.dropdown_refresh);
        // llLinearlayout.setVisibility(VISIBLE);
        progress.setVisibility(GONE);

    }

    @Override
    public void onUIRefreshPrepare(PtrFrameLayout frame) {
        //準備刷新
        mState = STATE_PREPARE;
        //llLinearlayout.setVisibility(GONE);


    }

    @Override
    public void onUIRefreshBegin(PtrFrameLayout frame) {
        //開始刷新 顯示刷新進度跟文本
        mState = STATE_BEGIN;
        progress.setVisibility(VISIBLE);
        start();


    }

    @Override
    public void onUIRefreshComplete(PtrFrameLayout frame) {
        //刷新完成  設置文本 設置進度隱藏
        mState = STATE_FINISH;
        //llLinearlayout.setVisibility(VISIBLE);
        progress.setVisibility(GONE);
        stop();
    }

    @Override
    public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {

        switch (mState) {
            case STATE_PREPARE:
                if (ptrIndicator.getCurrentPercent() < 0.4) {
                    mTvRemind.setText(R.string.dropdown_refresh);
                } else {
                    mTvRemind.setText(R.string.loosen_the_refresh);
                }
                break;
            case STATE_BEGIN:
                mTvRemind.setText(R.string.Refreshing);
                break;
            case STATE_FINISH:
                mTvRemind.setText(R.string.load_completion);
                break;
        }


    }


    /**
     * 開始播放
     */
    protected void start() {
        if (frameAnim != null && !frameAnim.isRunning()) {
            frameAnim.start();
        }
    }

    /**
     * 停止播放
     */
    protected void stop() {
        if (frameAnim != null && frameAnim.isRunning()) {
            frameAnim.stop();
        }
    }



}

這裏的 R.layout.refresh_header_view  佈局,就可以根據自己項目的需求進行修改。

4)使用自定義的下拉刷新頭部

final JdRefreshHeader header = new JdRefreshHeader(getContext());
       // header.setPadding(0, PtrLocalDisplay.dp2px(200), 0, 0);
        ptrframelayout.setHeaderView(header);        //設置刷新頭部
        ptrframelayout.addPtrUIHandler(header);
        //處理左右滑動衝突
        ptrframelayout.disableWhenHorizontalMove(true);

        ptrframelayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {

                return isrefresh;
                //return false;
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {


                //開始刷新--todo
                
            }


        });

 

5)以下是 android-Ultra-Pull-To-Refresh參數簡介 :

app:ptr_resistance="1.7":設置下拉的阻尼係數,值越大感覺越難下拉
ptr_ratio_of_header_height_to_refresh:設置超過頭部的多少時,釋放可以執行刷新操作
ptr_duration_to_close:設置下拉回彈的時間
ptr_duration_to_close_header:設置刷新完成,頭部回彈時間,注意和前一個進行區別
ptr_keep_header_when_refresh:設置刷新的時候是否保持頭部
ptr_pull_to_fresh:設置下拉過程中執行刷新,我們一般設置爲false

 

參考文檔:

  https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh/blob/master/README-cn.md

https://blog.csdn.net/shineflowers/article/details/59033536

 

 

 

 

 

 


 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章