Androi PtrFrameLayout实现下拉刷新

Ultra Pull To Refresh:https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh

Gradle依赖

//下拉刷新
compile 'in.srain.cube:ultra-ptr:1.0.11'

自定义PtrFrameLayout

public class MyPullRefreshFrameLayout extends PtrFrameLayout {
    public MyPullRefreshFrameLayout(Context context) {
        super(context);
        initViews();
    }

    public MyPullRefreshFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews();
    }

    public MyPullRefreshFrameLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initViews();
    }

    private void initViews() {
        ThreeDotScaleHeader header = new ThreeDotScaleHeader(getContext());
        setHeaderView(header);
        addPtrUIHandler(header);
    }

    private float startY;
    private float startX;
    // 记录viewPager是否拖拽的标记
    private boolean mIsHorizontalMove;
    // 记录事件是否已被分发
    private boolean isDeal;
    private int mTouchSlop = 0;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                // 记录手指按下的位置
                startY = ev.getY();
                startX = ev.getX();
                // 初始化标记
                mIsHorizontalMove = false;
                isDeal = false;
                break;
            case MotionEvent.ACTION_MOVE:
                // 如果已经判断出是否由横向还是纵向处理,则跳出
                if (isDeal) {
                    break;
                }
                /**拦截禁止交给Ptr的 dispatchTouchEvent处理**/
                mIsHorizontalMove = true;
                // 获取当前手指位置
                float endY = ev.getY();
                float endX = ev.getX();
                float distanceX = Math.abs(endX - startX);
                float distanceY = Math.abs(endY - startY);
                if (distanceX != distanceY) {
                    // 如果X轴位移大于Y轴位移,那么将事件交给viewPager处理
                    if (distanceX > mTouchSlop && distanceX > distanceY) {
                        mIsHorizontalMove = true;
                        isDeal = true;
                    } else if (distanceY > mTouchSlop) {
                        mIsHorizontalMove = false;
                        isDeal = true;
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                //下拉刷新状态时如果滚动了ViewPager 此时mIsHorizontalMove为true 会导致PtrFrameLayout无法恢复原位
                //初始化标记
                mIsHorizontalMove = false;
                isDeal = false;
                break;
        }
        if (mIsHorizontalMove) {
            return dispatchTouchEventSupper(ev);
        }
        return super.dispatchTouchEvent(ev);
    }
}

XML布局

<me.yezhou.lib.ui_widget.layout.MyPullRefreshFrameLayout
    android:id="@+id/layout_pull_refresh"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_order_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
        <include
            android:id="@+id/layout_order_empty"
            layout="@layout/layout_order_empty"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:visibility="gone"
            />
    </LinearLayout>
</me.yezhou.lib.ui_widget.layout.MyPullRefreshFrameLayout>

下拉监听

mLayoutPullRefresh.setPtrHandler(new PtrDefaultHandler() {
    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {

    }

    @Override
    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
        return super.checkCanDoRefresh(frame, mOrderListRecyclerView == null ? content : mOrderListRecyclerView, header);
        //return super.checkCanDoRefresh(frame, 需要刷新的当前View == null ? content : 需要刷新的当前View , header);
    }
});

停止刷新

mLayoutPullRefresh.refreshComplete();

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/11/android-ptrframelayout-implements-dropdown-refresh/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Androi PtrFrameLayout实现下拉刷新
Ultra Pull To Refresh:https://github.com/liaohuqiu/android-Ultra-Pull-To-Refresh Gradle依赖 //下拉刷新 compile 'in.srain.cube:ultra-ptr:1.0.11……
<<上一篇
下一篇>>
文章目录
关闭
目 录