您好,欢迎来到欧得旅游网。
搜索
您的当前位置:首页【Android -- 开源库】-->RecyclerView Animators 的基本使用

【Android -- 开源库】-->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<RecyclerView>(R.id.list)
recyclerView.itemAnimator = SlideInLeftAnimator()

or

val recyclerView = findViewById<RecyclerView>(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<RecyclerView>(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

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- ovod.cn 版权所有 湘ICP备2023023988号-4

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务