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

引言:仅作为笔记、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、布局文件:

android SmartRefreshLayout自定义刷新头、结合Lottie!(笔记)_第1张图片

 activity_main.xml 代码:





    

3、MainActivity

android SmartRefreshLayout自定义刷新头、结合Lottie!(笔记)_第2张图片

代码:

class MainActivity : AppCompatActivity() {

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

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

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

4、自定义Header

android SmartRefreshLayout自定义刷新头、结合Lottie!(笔记)_第3张图片

代码:

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.csdn.net/download/BirdEatBug/87404635 

 基本over!!

你可能感兴趣的:(android,开发,刷新头,android,Lottie,刷新头)