如何为RecyclerView设置动画(展开/折叠)

时间:2019-07-19 01:29:28

标签: android-recyclerview expandablerecyclerview

我今天在Kotlin和Im中启动了一个新应用程序,试图使用Recyclerview实现可扩展列表。我正在寻找一个流畅的动画。

哪种是实现此行为的最佳方法?

我现在与大家分享一下我所拥有的。库的两个常规动画功能。

打开:

        fun openHeight(animatedView : View?, animDuration : Long, finalHeight : Int,  onFinish: () -> Unit){
        animatedView?.apply{
            if(visibility!=View.VISIBLE && tag==null) {
                val newLayoutParams = layoutParams
                val anim = ValueAnimator.ofInt(0,finalHeight)
                anim.addUpdateListener { valueAnimator ->
                    val value= valueAnimator.animatedValue as Int
                    newLayoutParams.height = value
                    layoutParams = newLayoutParams
                    Log.d(TAG, "updateListener value=$value")
                }
                anim.addListener(object : AnimatorListenerAdapter(){
                    override fun onAnimationEnd(animation: Animator?, isReverse: Boolean) {
                        super.onAnimationEnd(animation, isReverse)
                        tag = null
                        Log.d(TAG, "onAnimationEnd")
                        onFinish()
                    }
                    override fun onAnimationStart(animation: Animator?, isReverse: Boolean) {
                        super.onAnimationStart(animation, isReverse)
                        Log.d(TAG, "onAnimationStart, measuredHeight=$measuredHeight")
                        newLayoutParams.height = 0
                        layoutParams = newLayoutParams
                        visibility = View.VISIBLE
                        tag = "isAnimating"
                    }
                })
                anim.duration = animDuration
                anim.interpolator = DecelerateInterpolator()
                anim.start()
            }else{
                onFinish()
            }
        }
    }

关闭:

        fun closeHeight(animatedView : View?, animDuration : Long, onFinish : () -> Unit){
        animatedView?.apply{
            if(visibility!=View.GONE && tag==null){
                var originalHeight = 0
                val newLayoutParams = layoutParams
                visibility = View.VISIBLE
                val anim = ValueAnimator.ofInt(measuredHeight, 0)
                anim.addUpdateListener { valueAnimator ->
                    val value= valueAnimator.animatedValue as Int
                    layoutParams.height = value
                    layoutParams = newLayoutParams
                    Log.d(TAG, "updateListener value=$value")
                }
                anim.addListener(object : AnimatorListenerAdapter(){
                    override fun onAnimationEnd(animation: Animator?, isReverse: Boolean) {
                        super.onAnimationEnd(animation, isReverse)
                        visibility = View.GONE
                        layoutParams.height = originalHeight
                        layoutParams = newLayoutParams
                        tag = null
                        Log.d(TAG, "onAnimationEnd")
                        onFinish()
                    }
                    override fun onAnimationStart(animation: Animator?, isReverse: Boolean) {
                        super.onAnimationStart(animation, isReverse)
                        Log.d(TAG, "onAnimationStart, measuredHeight=$measuredHeight")
                        originalHeight = measuredHeight
                        tag = "isAnimating"
                    }
                })
                anim.duration = animDuration
                anim.interpolator = DecelerateInterpolator()
                anim.start()
            }else{
                onFinish()
            }
        }
    }

0 个答案:

没有答案