Android

Android-推荐一个RecyclerView动画开源库( r

2022-09-17  本文已影响0人  阿博聊编程
图片来源网络,入侵必删

在日常的Android开发中,我们肯定要是用RecyclerView,为了更好的用户体验,我们可能会给RecyclerView添加动画效果。这篇博客给大家分享一个快速实现RecyclerView动效效果的开源库——recyclerview-animators,西能能帮大家提高开发效率。

引入项目

dependencies {
  implementation 'jp.wasabeef:recyclerview-animators:4.0.2'
}

在我发布博客的时候,开源库版本是4.0.2后续的最新版本请看开源库的文档

开源库效果

动画效果.gif

使用示例

步骤一

val recyclerView = findViewById<RecyclerView>(R.id.list)
recyclerView.itemAnimator = SlideInLeftAnimator()

或者

val recyclerView = findViewById<RecyclerView>(R.id.list)
recyclerView.itemAnimator = SlideInUpAnimator(OvershootInterpolator(1f))

步骤二

建议使用以下几种刷新方式:

注意:使用notifyDataSetChanged()会导致动画无效。

fun remove(position: Int) {
  dataSet.removeAt(position)
  notifyItemRemoved(position)
}

fun add(text: String, position: Int) {
  dataSet.add(position, text)
  notifyItemInserted(position)
}

步骤三

设置插值器:

recyclerView.itemAnimator = SlideInLeftAnimator().apply {
  setInterpolator(OvershootInterpolator())
}

步骤四

通过实现AnimateViewHolder覆盖预设动画。我们可以根据视图保持器设置自定义动画。

class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), AnimateViewHolder {

  override fun preAnimateRemoveImpl(holder: RecyclerView.ViewHolder) {
    // do something
  }

  override fun animateRemoveImpl(holder: RecyclerView.ViewHolder, listener: ViewPropertyAnimatorListener) {
    itemView.animate().apply {
      translationY(-itemView.height * 0.3f)
      alpha(0f)
      duration = 300
      setListener(listener)
    }.start()
  }

  override fun preAnimateAddImpl(holder: RecyclerView.ViewHolder) {
    itemView.setTranslationY(-itemView.height * 0.3f)
    itemView.setAlpha(0f)
  }

  override fun animateAddImpl(holder: RecyclerView.ViewHolder, listener: ViewPropertyAnimatorListener) {
    itemView.animate().apply {
      translationY(0f)
      alpha(1f)
      duration = 300
      setListener(listener)
    }.start()
  }
}

关于支持的动画

Cool

LandingAnimator

Scale

ScaleInAnimator, ScaleInTopAnimator, ScaleInBottomAnimator,ScaleInLeftAnimator, ScaleInRightAnimator

Fade

FadeInAnimator, FadeInDownAnimator, FadeInUpAnimator,FadeInLeftAnimator,FadeInRightAnimator

Flip

FlipInTopXAnimator, FlipInBottomXAnimator,FlipInLeftYAnimator, FlipInRightYAnimator

Slide

SlideInLeftAnimator, SlideInRightAnimator, OvershootInLeftAnimator, OvershootInRightAnimator,SlideInUpAnimator, SlideInDownAnimator

RecyclerView.Adapter

步骤一

val recyclerView = findViewById<RecyclerView>(R.id.list)
recyclerView.adapter = AlphaInAnimationAdapter(MyAdapter())

步骤二

recyclerView.adapter = AlphaInAnimationAdapter(MyAdapter()).apply {
  // Change the durations.
  setDuration(1000)
  // Change the interpolator.
  setInterpolator(vershootInterpolator())
  // Disable the first scroll mode.
  setFirstOnly(false)
}

步骤三

多重动画,可选:

val alphaAdapter = AlphaInAnimationAdapter(MyAdapter())
recyclerView.adapter = ScaleInAnimationAdapter(alphaAdapter)

Adapters

Alpha

AlphaInAnimationAdapter

Scale

ScaleInAnimationAdapter

Slide

SlideInBottomAnimationAdapter,SlideInRightAnimationAdapter, SlideInLeftAnimationAdapter

上一篇下一篇

猜你喜欢

热点阅读