您当前的位置: 首页 >  android

Kevin-Dev

暂无认证

  • 1浏览

    0关注

    544博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Android -- 开源库】-->RecyclerView Animators 的基本使用

Kevin-Dev 发布时间:2022-04-12 08:15:00 ,浏览量:1

在这里插入图片描述

前言

RecyclerView Animators 是一个 Android 库,允许开发人员轻松创建带有动画的 RecyclerView

  • 动画添加和删除 ItemAnimator
  • RecyclerView.Adapter 中项目的外观动画
效果图
  • ItemAnimator 在这里插入图片描述 在这里插入图片描述

  • Adapters 在这里插入图片描述 在这里插入图片描述

使用 1. 添加依赖
dependencies {
  // Kotlin
  implementation 'jp.wasabeef:recyclerview-animators:4.x.x'
  // or
  // Java
  implementation 'jp.wasabeef:recyclerview-animators:3.x.x'
}

还要确保 repositories 部分不仅包括 jcenter,而且还包括带有 “google()” 端点的 maven 部分。

repositories {
  google()
  jcenter()
}
2. ItemAnimator

2.1 设置 RecyclerViewItemAnimator

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

or

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

2.2 建议使用以下 notifyItemChanged(int) notifyItemInserted(int) notifyItemRemoved(int) notifyItemRangeChanged(int, int) notifyItemRangeInserted(int, int) notifyItemRangeRemoved(int, int)

2.3 设置动画时长

recyclerView.itemAnimator?.apply {
  addDuration = 1000
  removeDuration = 100
  moveDuration = 1000
  changeDuration = 100
}

2.4 添加插值器

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

2.5 在 ViewHolder 实现 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()
  }
}
Animators
  • 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

3. RecyclerView.Adapter

3.1 设置 ItemAnimator

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

3.2 设置动画时长

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

3.3 多重动画(可选)

val alphaAdapter = AlphaInAnimationAdapter(MyAdapter())
recyclerView.adapter = ScaleInAnimationAdapter(alphaAdapter)
Adapters
  • Alpha AlphaInAnimationAdapter

  • Scale ScaleInAnimationAdapter

  • Slide SlideInBottomAnimationAdapter SlideInRightAnimationAdapter, SlideInLeftAnimationAdapter

关注
打赏
1658837700
查看更多评论
立即登录/注册

微信扫码登录

0.0379s