ptr 下拉刷新

目标:实现下拉刷新功能,并解决和list view的冲突。
导入依赖:

dependencies{
	implementation 'in.srain.cube:ultra-ptr:1.0.11'// 下拉刷新
}

准备工作:
1.构建帮助类PtrInitHelper:

public class PtrInitHelper {

    public static void initPtr(Context context, final PtrFrameLayout ptrFrameLayout){
        final MaterialHeader header = new MaterialHeader(context);
        int[] colors = context.getResources().getIntArray(R.array.google_colors);
        header.setColorSchemeColors(colors);
        header.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, -2));
        header.setPadding(0, (int) DensityUtils.dp2px(context, 30), 0, (int) DensityUtils.dp2px(context, 20));
        header.setPtrFrameLayout(ptrFrameLayout);

        ptrFrameLayout.setLoadingMinTime(100);
        ptrFrameLayout.setHeaderView(header);
        ptrFrameLayout.addPtrUIHandler(header);
        ptrFrameLayout.disableWhenHorizontalMove(true);
    }
}

2.定制的NotifyingScrollView:

public class NotifyingScrollView extends ScrollView{

    private boolean mDisableEdgeEffects = true;

    public interface OnScrollChangedListener {
        void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt);
    }

    // 滑动距离监听器
    public interface OnScrollListener{
        // 滑动时调用,scrollY为已滑动距离
        void onScroll(int scrollY);
    }

    private OnScrollChangedListener mOnScrollChangedListener;

    public NotifyingScrollView(Context context) {
        super(context);
    }

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

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

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mOnScrollChangedListener != null){
            mOnScrollChangedListener.onScrollChanged(this, l, t, oldl, oldt);
        }
    }

    public void setOnScrollChangedListener(OnScrollChangedListener listener){
        mOnScrollChangedListener = listener;
    }

    @Override
    protected float getTopFadingEdgeStrength() {
        if (mDisableEdgeEffects && Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
            return 0.0f;
        }
        return super.getTopFadingEdgeStrength();
    }
}

3.定制的listview:

public class ListViewForScrollView extends ListView {

    public ListViewForScrollView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public ListViewForScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public ListViewForScrollView(Context context, AttributeSet attrs,
                                 int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

准备工作做完后着手具体实现
1.MainActivity的布局:

<in.srain.cube.views.ptr.PtrFrameLayout
        android:id="@+id/ptr_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

		<com.xxx.xxx.NotifyingScrollView
                android:id="@+id/ot_sv"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="5dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <com.xxx.xxx.view.ListViewForScrollView
                        android:id="@+id/order_list_layout"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:focusable="true" />

                </LinearLayout>
            </com.xxx.xxx.view.NotifyingScrollView>
</in.srain.cube.views.ptr.PtrFrameLayout>

2.MainActivity类:

@BindView(R.id.ptr_frame)
PtrFrameLayout ptrFrame;
    
@BindView(R.id.ot_sv)
NotifyingScrollView mScroll;

@BindView(R.id.order_list_layout)
ListViewForScrollView lv_order;

...

public void initData{
	...
	PtrInitHelper.initPtr(getActivity(), ptrFrame);
    mScroll.scrollTo(0, 0);
    
    ptrFrame.disableWhenHorizontalMove(true);
        ptrFrame.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                Logger.e("do something");
                doHttpCall();
                ptrFrame.refreshComplete();// 结束刷新
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return super.checkCanDoRefresh(frame, mScroll, header);
            }
        });
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值