SmartRefreshLayout和Ultra Pull To Refresh自定义刷新头布局

简介

毕业后第一家公司上班已经三天啦,处于代码交接的尴尬时期,没啥具体工作,看了一下几乎每个项目中都会用到的下拉刷新和上拉加载更多,目前应用最多的应该是Ultra Pull To Refresh了,这个框架很强大(除了没有上拉加载过程,但是作者说这不是这个空间应该有的功能,好吧...),可以自定义下拉刷新布局,并提供了简单易懂的下拉刷新接口,供开发者自己实现。后再掘金上发现了另外一个强大的刷新框架SmartRefreshLayout,该框架应该也是参照了前者的思路,不过强大的是,可以自动识别头尾刷新布局,并且直接应用到刷新空间中,虽然没有对外提供方法,不过提供了很多种默认的实现。仿照默认实现也可自己定制。

效果图

SmartRefreshLayout和Ultra Pull To Refresh自定义刷新头布局_第1张图片
GIF.gif

Pull To Refresh 实现方式

public class JdFreshHead extends FrameLayout implements PtrUIHandler {

    private ImageView iv_man;
    private ImageView iv_goods;
    private TextView tv_refresh;
    private int state;
    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 JdFreshHead(@NonNull Context context) {

        super(context);
        initView();
    }


    public JdFreshHead(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.refresh_head, this, true);
        iv_man = view.findViewById(R.id.iv_man);
        iv_goods = view.findViewById(R.id.iv_goods);
        tv_refresh = ((TextView) view.findViewById(R.id.tv_remain));
    }



    @Override
    public void onUIReset(PtrFrameLayout frame) {
        state=STATE_RESET;
    }

    @Override
    public void onUIRefreshPrepare(PtrFrameLayout frame) {
        state=STATE_PREPARE;
    }

    @Override
    public void onUIRefreshBegin(PtrFrameLayout frame) {
        state=STATE_BEGIN;
        iv_goods.setVisibility(GONE);
        iv_man.setBackgroundResource(R.drawable.running);
        AnimationDrawable background = (AnimationDrawable) iv_man.getBackground();
        if (!background.isRunning()) {
            background.start();
        }
    }

    @Override
    public void onUIRefreshComplete(PtrFrameLayout frame) {
        state=STATE_FINISH;
        AnimationDrawable drawable = (AnimationDrawable) iv_man.getBackground();
        if (drawable.isRunning()) {
            drawable.stop();
        }
    }

    @Override
    public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
        switch (state){
            case STATE_PREPARE:

                if (ptrIndicator.getCurrentPercent()<1.2){
                    tv_refresh.setText("下拉刷新.....");

                }else {
                    tv_refresh.setText("松开刷新.....");
                }
                if (ptrIndicator.getCurrentPercent()<1){
                    FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) iv_man.getLayoutParams();
                    layoutParams.setMargins(0,0, (int) ((1-ptrIndicator.getCurrentPercent())*300),0);
                    iv_man.setLayoutParams(layoutParams);
                    iv_man.setAlpha(ptrIndicator.getCurrentPercent());
                    iv_man.setScaleX(ptrIndicator.getCurrentPercent());
                    iv_man.setScaleY(ptrIndicator.getCurrentPercent());
                    iv_goods.setAlpha(ptrIndicator.getCurrentPercent());
                    iv_goods.setScaleX(ptrIndicator.getCurrentPercent());
                    iv_goods.setScaleY(ptrIndicator.getCurrentPercent());
                }


                break;
            case STATE_BEGIN:
                tv_refresh.setText("更新中  .....");
                iv_goods.setVisibility(GONE);
                break;
            case STATE_FINISH:
                tv_refresh.setText("刷新完成.....");
                iv_man.setBackgroundResource(R.drawable.person);
                iv_goods.setVisibility(VISIBLE);
                break;
            case STATE_RESET:
                tv_refresh.setText("刷新重置.....");
                break;

        }
    }
}
public class JDReFreshLayout extends PtrFrameLayout {

    private JdFreshHead jdFreshHead;

    public JDReFreshLayout(Context context) {
        super(context);
        init();
    }



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

    private void init() {
        jdFreshHead = new JdFreshHead(getContext());
        setHeaderView(jdFreshHead);
        addPtrUIHandler(jdFreshHead);
    }

}

SmartRefreshLayout(实现RefreshHeader接口自动识别为刷新头)

public class JdSmartHead extends FrameLayout implements RefreshHeader {
    private static final String TAG = "1111111111";
    private View mContent;
    private ImageView iv_man;
    private ImageView iv_goods;
    private TextView tv_remain;  //刷新提示文字
    private LayoutParams mContentLayoutParams;
    private LayoutParams iv_manLayoutParams;
    private AnimationDrawable drawable;

    public JdSmartHead(Context context) {
        super(context, null);
    }

    public JdSmartHead(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContent = LayoutInflater.from(context).inflate(R.layout.refresh_head, null);
        iv_man = mContent.findViewById(R.id.iv_man);
        iv_goods = mContent.findViewById(R.id.iv_goods);
        tv_remain = mContent.findViewById(R.id.tv_remain);
        addView(mContent);
    }

    @Override
    public void onPullingDown(float percent, int offset, int headerHeight, int extendHeight) {
        Log.d(TAG, "onPullingDown: percent :" + percent + "   headHeight  " + headerHeight);
        mContentLayoutParams = (LayoutParams) mContent.getLayoutParams();
        mContentLayoutParams.topMargin = (int) (-(1 - percent) * headerHeight);
        mContent.setLayoutParams(mContentLayoutParams);

        if (percent<1){
            iv_goods.setScaleY(percent);
            iv_goods.setScaleX(percent);
            iv_man.setScaleY(percent);
            iv_man.setScaleX(percent);
            iv_manLayoutParams = ((LayoutParams) iv_man.getLayoutParams());
            iv_manLayoutParams.rightMargin= (int) (200*(1-percent));
            iv_man.setLayoutParams(iv_manLayoutParams);
            tv_remain.setText("下拉刷新....");
        }else {
            tv_remain.setText("释放刷新....");
        }


    }

    @Override
    public void onReleasing(float percent, int offset, int headerHeight, int extendHeight) {
        Log.d(TAG, "onReleasing: percent  :" + percent);

        mContentLayoutParams = (LayoutParams) mContent.getLayoutParams();
        mContentLayoutParams.topMargin = (int) (-(1 - percent) * headerHeight);
        mContent.setLayoutParams(mContentLayoutParams);

        if (percent<1){
            iv_goods.setScaleY(percent);
            iv_goods.setScaleX(percent);
            iv_man.setScaleY(percent);
            iv_man.setScaleX(percent);
        }else {
            tv_remain.setText("刷新中....");
        }

    }

    @NonNull
    @Override
    public View getView() {
        Log.d(TAG, "getView: ");
        return this;
    }


    @Override
    public SpinnerStyle getSpinnerStyle() {
//        Log.d(TAG, "getSpinnerStyle: ");
        return null;
    }

    @Override
    public void setPrimaryColors(int... colors) {
//        Log.d(TAG, "setPrimaryColors: ");
    }

    @Override
    public void onInitialized(RefreshKernel kernel, int height, int extendHeight) {
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mContent.getLayoutParams();
        layoutParams.topMargin = -height;
        Log.d(TAG, "onInitialized: ");
    }

    @Override
    public void onStartAnimator(RefreshLayout layout, int height, int extendHeight) {
       iv_man.setBackgroundResource(R.drawable.running);
        drawable = (AnimationDrawable) iv_man.getBackground();
        if (!drawable.isRunning())
        drawable.start();
        iv_goods.setVisibility(GONE);
    }

    @Override
    public int onFinish(RefreshLayout layout, boolean success) {
        if (drawable.isRunning()) {
            drawable.stop();
            drawable=null;
        }
        iv_goods.setVisibility(VISIBLE);
        iv_man.setBackgroundResource(R.drawable.person);
        return 0;
    }

    @Override
    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
        Log.d(TAG, "onStateChanged: ");
    }

}

你可能感兴趣的:(SmartRefreshLayout和Ultra Pull To Refresh自定义刷新头布局)