Android PtrFrameLayout实现下拉刷新与上拉加载更多

CommonPullToRefresh:https://github.com/Chanven/CommonPullToRefresh

Gradle依赖

//下拉刷新及上拉加载
compile 'com.chanven.lib:cptr:1.1.0'

修改PtrFrameLayout

修改PtrFrameLayout的setLoadMoreEnable方法,以支持LinearLayout包裹GridView、ListView、RecyclerView实现上拉加载更多。

public void setLoadMoreEnable(boolean loadMoreEnable) {
    ...

    if (null == mLoadMoreHandler) {
        if (mContentView instanceof GridView) {
            mLoadMoreHandler = new GridViewHandler();
        } else if (mContentView instanceof AbsListView) {
            mLoadMoreHandler = new ListViewHandler();
        } else if (mContentView instanceof RecyclerView) {
            mLoadMoreHandler = new RecyclerViewHandler();
        } else if (mContentView instanceof LinearLayout && ((LinearLayout) mRootView).getChildCount() > 0) {
            View childView = ((LinearLayout) mRootView).getChildAt(0);
            if (childView instanceof GridView) {
                mContentView = childView;
                mLoadMoreHandler = new GridViewHandler();
            } else if (childView instanceof AbsListView) {
                mContentView = childView;
                mLoadMoreHandler = new ListViewHandler();
            } else if (childView instanceof RecyclerView) {
                mContentView = childView;
                mLoadMoreHandler = new RecyclerViewHandler();
            }
        }
    }

    ...
}

自定义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);

        ILoadMoreViewFactory loadMoreViewFactory = new DefaultLoadMoreViewFooter();
        setFooterView(loadMoreViewFactory);
    }

    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>

设置监听器

mOrderListAdapter = new OrderListAdapter(mActivity, mOrderList);
mOrderListAdapterWithHF = new RecyclerAdapterWithHF(mOrderListAdapter);
mOrderListRecyclerView.setAdapter(mOrderListAdapterWithHF);

下拉监听

mLayoutPullRefresh.setPtrHandler(new PtrDefaultHandler() {
    @Override
    public void onRefreshBegin(PtrFrameLayout frame) {
        mIsPullRefresh = true;

    }

    @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.setOnLoadMoreListener(new OnLoadMoreListener() {
    @Override
    public void loadMore() {
        mIsPullRefresh = false;

    }
});

停止刷新

@Override
public void onGetOrderListSuccess(List<Order> orderList, int[] totals) {
    if (mIsPullRefresh) {
        mLayoutPullRefresh.refreshComplete();
        mLayoutPullRefresh.setLoadMoreEnable(true);
    } else {
        if (orderList.size() < totals[0]) {
            mLayoutPullRefresh.loadMoreComplete(true);
        } else {
            mLayoutPullRefresh.loadMoreComplete(false);
        }
    }
}

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

THE END
分享
二维码
打赏
海报
Android PtrFrameLayout实现下拉刷新与上拉加载更多
CommonPullToRefresh:https://github.com/Chanven/CommonPullToRefresh Gradle依赖 //下拉刷新及上拉加载 compile 'com.chanven.lib:cptr:1.1.0' ……
<<上一篇
下一篇>>
文章目录
关闭
目 录