美文网首页
Android 自定义刷新头和底部刷新

Android 自定义刷新头和底部刷新

作者: 资本家大恶人 | 来源:发表于2020-08-20 14:45 被阅读0次

1.添加刷新底部和头部依赖

implementation deps.other.smartRefreshLayout
 implementation deps.other.smartRefreshHeader

2.在Application里面构造全局(避免在每个页面添加Head,Foot 的Xml布局)

    //static 代码段可以防止内存泄露
    static {
//   构造全局的刷新头
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) {
                return (RefreshHeader) LayoutInflater.from(context).inflate(R.layout.layout_refresh_header, null, false);
            }

        });
//   构造全局的foot
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @NonNull
            @Override
            public RefreshFooter createRefreshFooter(@NonNull Context context, @NonNull RefreshLayout layout) {
                return (RefreshFooter) LayoutInflater.from(context).inflate(R.layout.layout_refresh_footer, null, false);
            }
        });
    }

3 添加自定义View

  • SmartRefreshFooter

XML

<?xml version="1.0" encoding="utf-8"?>

<com.angle.jiandao.wedgit.SmartRefreshFooter
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:background="@color/white"
    android:layout_height="wrap_content">


    <com.cunoraz.gifview.library.GifView
        android:id="@+id/gifView"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        app:gif="@drawable/mvp_loading"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/maskView"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />
</com.angle.jiandao.wedgit.SmartRefreshFooter>

自定义Head


public class SmartRefreshFooter extends ConstraintLayout implements RefreshFooter {
    // 添加Gif动画
    private GifView mGifView;
    private ImageView mMaskView;
    private DecimalFormat decimalFormat = new DecimalFormat("000");

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

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

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

    //  在布局完成初始化时找到该控件
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
//        实例化所有控件并隐藏Gif
        mGifView = findViewById(R.id.gifView);
        mMaskView = findViewById(R.id.maskView);
        mGifView.setVisibility(INVISIBLE);
        mGifView.pause();
    }

    @NonNull
    @Override
    public View getView() {
        return this;
    }

    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;
    }

    @Override
    public void setPrimaryColors(int... colors) {
    }

    @Override
    public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {

    }

    /**
     * @param isDragging    手指手否在界面移动
     * @param percent
     * @param offset
     * @param height
     * @param maxDragHeight 在该操作中做下啦适量动画
     */
    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
        Logger.d("%s,isDragging = %s,percent = %s", "footer", isDragging, percent);
//        if (isDragging && percent <= 1) {
//            if(!mGifView.isPlaying()){
//                mGifView.setVisibility(INVISIBLE);
//                mMaskView.setVisibility(VISIBLE);
//                int p = (int) (percent * 100) / 2; // (0 ~ 50)
//                String prefixName = "header" + decimalFormat.format(p);
//                int id = getResources().getIdentifier(prefixName, "drawable", getContext().getPackageName());
//                if (id > 0) {
//                    mMaskView.setBackgroundResource(id);
//                }
//            }else{
//                mMaskView.setVisibility(INVISIBLE);
//            }


//        }

    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

    }

    //  开始刷新时显示动画
    @Override
    public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
        Logger.d(" State onStartAnimator ");
        mMaskView.setVisibility(INVISIBLE);
        mGifView.setVisibility(VISIBLE);
        mGifView.play();
    }

    //    刷新完成是停止动画
    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
        Logger.d();
        Logger.d(" State onFinish ");
        mGifView.pause();
        return 500; //  加载完成后多久关闭,即回弹会去
    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
        Logger.d("%s oldState = %s newState  = %s", "onStateChanged", oldState.name(), newState.name());
        //switch ()

    }

    @Override
    public boolean setNoMoreData(boolean noMoreData) {
        return false;
    }
}

  • SmartRefreshHeader

XML

<?xml version="1.0" encoding="utf-8"?>

<com.angle.jiandao.wedgit.SmartRefreshHeader
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:background="@color/white"
    android:layout_height="wrap_content">


    <com.cunoraz.gifview.library.GifView
        android:id="@+id/gifView"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        app:gif="@drawable/mvp_loading"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/maskView"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />
</com.angle.jiandao.wedgit.SmartRefreshHeader>

自定义头部


public class SmartRefreshHeader extends ConstraintLayout implements RefreshHeader {

    private GifView mGifView;
    private ImageView mMaskView;
    private DecimalFormat decimalFormat = new DecimalFormat("000");

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

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

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


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mGifView = findViewById(R.id.gifView);
        mMaskView = findViewById(R.id.maskView);
        mGifView.setVisibility(INVISIBLE);
        mGifView.pause();
    }

    @NonNull
    @Override
    public View getView() {
        return this;
    }

    @NonNull
    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;
    }

    @Override
    public void setPrimaryColors(int... colors) {
    }

    @Override
    public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {

    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
//        mGifView.setVisibility( offset > 1 ? INVISIBLE : VISIBLE);
//        mMaskView.setVisibility(offset > 1 ? VISIBLE : INVISIBLE);
//        if (isDragging && percent <= 1) {
//            int p = (int) (percent * 100) / 2; // (0 ~ 50)
//            String prefixName = "header" + decimalFormat.format(p);
//            int id = getResources().getIdentifier(prefixName, "drawable", getContext().getPackageName());
//            if (id > 0) {
//                mMaskView.setBackgroundResource(id);
//            }
//        }

    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

    }

    @Override
    public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
        mMaskView.setVisibility(INVISIBLE);
        mGifView.setVisibility(VISIBLE);
        mGifView.play();
    }

    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
        mGifView.pause();
        return 500;
    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {

        //switch ()
    }
}

相关文章

网友评论

      本文标题:Android 自定义刷新头和底部刷新

      本文链接:https://www.haomeiwen.com/subject/kwwsjktx.html