Android 高仿UC下拉刷新的实现

首先看一下UC的效果:

在这里插入图片描述--------------在这里插入图片描述

看下仿后的效果:

在这里插入图片描述------------在这里插入图片描述

以下是实现:

用的是baseQuick+SmartRefresh+lottie实现的;Lottie需要androidx+lottie3.0,如果不是的话需要求助UI用AE修改json为旧版本。
主要用了smartRefresh的自定义header,然后就完成了
lottie-------------uc的头没有用
BaseQuickAdapter-----
SmartRefreshLayout
在这里插入图片描述
1.以下是自定义header的方法,需要一个header的xml,我高度设置为70dp,感觉刚刚好,
然后在Moving方法中中监听percent来控制控件的显隐。上代码:

public class MyRefreshHeader extends LinearLayout implements RefreshHeader {
    LottieAnimationView mAnimationView;
    TextView tv_success;
    TextView tv_fail;
    
    public MyRefreshHeader(Context context) {
        super(context);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.loading_lottie, this);
        mAnimationView = view.findViewById(R.id.loading_lottie);
        tv_success = view.findViewById(R.id.tv_success);
        tv_fail = view.findViewById(R.id.tv_fail);
    }

    /**
     * 注意不能为null
     * @return
     */
    @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) {

        if (percent<0.03){
            tv_success.setVisibility(GONE);
            tv_fail.setVisibility(GONE);
            mAnimationView.setVisibility(VISIBLE);
        }
    }

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

    }

    @Override
    public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
        mAnimationView.playAnimation();

    }

    @Override
    public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
        mAnimationView.setVisibility(GONE);
        if (success){
            tv_success.setVisibility(VISIBLE);
        }else {
            tv_fail.setVisibility(VISIBLE);
        }
        mAnimationView.cancelAnimation();
        return 1000;  //延迟1000毫秒之后再弹回
    }

    @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) {
        
    }

}

贴出xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:gravity="center"
    android:orientation="vertical">

    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/loading_lottie"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_gravity="center_horizontal"
        app:lottie_fileName="loading_uc.json"
        app:lottie_loop="true" />

    <TextView
        android:id="@+id/tv_success"
        android:layout_width="200dp"
        android:layout_height="30dp"
        android:background="@drawable/shape_refresh_success"
        android:gravity="center"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="已为您刷新"
        android:textColor="#777777"
        android:textSize="12sp"
        android:visibility="gone" />

    <TextView
        android:id="@+id/tv_fail"
        android:layout_width="200dp"
        android:layout_height="30dp"
        android:background="@drawable/shape_refresh_fail"
        android:gravity="center"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="网络连接失败"
        android:textColor="#ffffff"
        android:textSize="12sp"
        android:visibility="gone" />
</LinearLayout>

然后就是MainActivity了:

public class MainActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final SmartRefreshLayout srl_main = findViewById(R.id.srl_main);
        RecyclerView rv_main = findViewById(R.id.rv_main);
        //动态设置的header
        MyRefreshHeader myRefreshHeader = new MyRefreshHeader(this);
        srl_main.setHeaderHeight(70.0f);
        srl_main.setHeaderTriggerRate(0.8f);
        srl_main.setRefreshHeader(myRefreshHeader);
        
        ArrayList<String> data = new ArrayList<>();
        data.add("第一个");
        data.add("第一个");
        data.add("第一个");
        data.add("第一个");
        data.add("第一个");
        BaseQuickAdapter<String, BaseViewHolder> adapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_main,data) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, String item) {

            }
        };
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        rv_main.setLayoutManager(linearLayoutManager);
        rv_main.setAdapter(adapter);

        srl_main.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        srl_main.finishRefresh(true);
                    }
                },2000);
            }
        });
    }


}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值