android SmartRefreshLayout自定义刷新头、结合Lottie!(笔记)

本文介绍如何结合SmartRefreshLayout刷新框架与Lottie动画库来创建自定义的刷新头动画效果。文中提供了从添加依赖到具体实现步骤的详细代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引言:仅作为笔记、demo来源于SmartRefreshLayout 官网、稍作修改、引用地址:SmartRefreshLayout/md_custom.md at master · scwang90/SmartRefreshLayout · GitHub

这里使用 SmartRefreshLayout、以及Lottie两个库(Lottie也可以用svga替换);

先看效果吧!

解释:很简单、上面的动画是个json文件、这个文件被Lottie解析播放就行了、其他按照 SmartRefreshLayout 官网自定义header写就行!

废话不多说、粘出关键代码及目录结构;

1、添加依赖:


     // SmartRefreshLayout 刷新框架
    implementation 'io.github.scwang90:refresh-layout-kernel:2.0.5'
    //Lottie
    implementation "com.airbnb.android:lottie:5.2.0"

2、布局文件:

 activity_main.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<com.scwang.smart.refresh.layout.SmartRefreshLayout
    android:id="@+id/srl"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@android:color/darker_gray"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</com.scwang.smart.refresh.layout.SmartRefreshLayout>

3、MainActivity

代码:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val srl = findViewById<SmartRefreshLayout>(R.id.srl)
        srl.setOnRefreshListener {
            srl.postDelayed({
                srl.finishRefresh()
            }, 2000)
        }

        srl.setRefreshHeader(AndroidViewHeader(this))
        srl.setEnableRefresh(true)
        srl.setHeaderMaxDragRate(2f)
    }
}

4、自定义Header

代码:

class AndroidViewHeader : LinearLayout, RefreshHeader {

    private var mAct: Activity? = null
    private var mLottieAnimationView: LottieAnimationView? = null
    private var mHasInit = false
//    private var mainPageCtrl: MainPageCtrl? = null

    private var kernel: RefreshKernel? = null

    private var mDesColor: Int = 0

    constructor(context: Context):super(context){
        mAct=context as Activity
    }

    constructor(context: Context, attr: AttributeSet) : super(context, attr)

    constructor(context: Context, attr: AttributeSet, def: Int) : super(context, attr, def)

    private fun initLoiite() {
        val layoutParams = LayoutParams(LayoutParams.MATCH_PARENT,400)
        mLottieAnimationView = LottieAnimationView(mAct)
        mLottieAnimationView!!.layoutParams = layoutParams
        mLottieAnimationView!!.loop(true)
        mLottieAnimationView?.setFailureListener { t ->
            Log.d("xxxxxxxxx","error-> $t")
        }
       /* val source = StringBuilder()
        var open: InputStream? = null
        var bufferedReader: BufferedReader? = null
        try {
            open = mAct!!.assets.open("lotties/h_a.json")
            bufferedReader = BufferedReader(InputStreamReader(open))
            var line: String?
            while (bufferedReader.readLine().also { line = it } != null) {
                Log.d("xxxxxxxxx","dowhite-line $line")
                source.append(line)
            }
            open.close()
            bufferedReader.close()
            Log.d("xxxxxxxxx","source $source")
            mLottieAnimationView?.setAnimation(source.toString())
        } catch (e: IOException) {
            e.printStackTrace()
        } catch (e: JSONException) {
            e.printStackTrace()
        } finally {
            try {
                open?.close()
                bufferedReader?.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }*/
        mLottieAnimationView?.setAnimation(R.raw.android_wave)
        gravity = Gravity.CENTER
        orientation = VERTICAL
        addView(mLottieAnimationView)
    }


    @SuppressLint("RestrictedApi")
    override fun onStateChanged(
        refreshLayout: RefreshLayout,
        oldState: RefreshState,
        newState: RefreshState
    ) {
        when (newState) {
            RefreshState.None -> {}
            RefreshState.PullDownToRefresh -> {}
            RefreshState.PullUpToLoad -> {}
            RefreshState.PullDownCanceled -> {}
            RefreshState.PullUpCanceled -> {}
            RefreshState.ReleaseToRefresh -> {}
            RefreshState.ReleaseToLoad -> {}
            RefreshState.ReleaseToTwoLevel -> {}
            RefreshState.TwoLevelReleased -> {}
            RefreshState.RefreshReleased -> {}
            RefreshState.LoadReleased -> {}
            RefreshState.Refreshing -> {}
            RefreshState.Loading -> {}
            RefreshState.TwoLevel -> {}
            RefreshState.RefreshFinish -> {}
            RefreshState.LoadFinish -> {}
            RefreshState.TwoLevelFinish -> {}
        }
    }

    override fun getView() = this

    override fun getSpinnerStyle(): SpinnerStyle = SpinnerStyle.Translate

    @SuppressLint("RestrictedApi")
    override fun setPrimaryColors(vararg colors: Int) {

    }

    @SuppressLint("RestrictedApi")
    override fun onInitialized(kernel: RefreshKernel, height: Int, maxDragHeight: Int) {
        this.kernel = kernel
        kernel.requestDrawBackgroundFor(
            this,
            if (mDesColor != 0) mDesColor else Color.parseColor("#ffffff")
        )
    }

    @SuppressLint("RestrictedApi")
    override fun onMoving(
        isDragging: Boolean,
        percent: Float,
        offset: Int,
        height: Int,
        maxDragHeight: Int
    ) {
        if (!mHasInit) {
            initLoiite()
            mHasInit = true;
        }
    }

    @SuppressLint("RestrictedApi")
    override fun onReleased(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {
        Log.d("xxxxxxxxx","onReleased")
        mLottieAnimationView?.playAnimation();
    }

    @SuppressLint("RestrictedApi")
    override fun onStartAnimator(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {

    }

    @SuppressLint("RestrictedApi")
    override fun onFinish(refreshLayout: RefreshLayout, success: Boolean): Int {
//        mainPageCtrl.onResume()
        return 0
    }

    @SuppressLint("RestrictedApi")
    override fun onHorizontalDrag(percentX: Float, offsetX: Int, offsetMax: Int) {
    }

    override fun isSupportHorizontalDrag() = false
}

 Lottie文件:https://download.youkuaiyun.com/download/BirdEatBug/87404635 

 基本over!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值