OpenHarmony UI动画实战开发-recyclerview_animators

往期知识点整理

简介

带有添加删除动画效果以及整体动画效果的list组件库

下载安装

ohpm install @ohos/recyclerview-animators

使用说明

  1. 引入组件库
import { RecyclerView } from "@ohos/recyclerview-animators"
  1. 在代码中使用
@State controller: RecyclerView.Controller = new RecyclerView.Controller()
private listDatas = ["A","B","C"]

aboutToAppear() {
    this.controller.setAdapterAnimation(RecyclerView.AdapterAnimationType.AlphaIn) // 设置列表整体效果类型
    this.controller.setFirstOnly(false) // 设置是否在item重复出现时显示动画效果
    this.controller.setDuration(500) // 设置动画时长
}

build() {
  Column() {
    RecyclerView({
      array: this.listDatas, // 数据源
      controller: this.controller, // 控制器
      child: (itemData) => {
        this.SpecificChild(itemData) // 子布局
      }
    })
  }
}

@Builder SpecificChild(itemData) {
    Column() {
      Image($r("app.media.chip"))
        .width('100%')
        .height(100)
      Text(itemData + '')
        .fontSize(20)
        .textAlign(TextAlign.Center)
        .width('100%')
    }.margin(10)
  }

接口说明

controller: RecyclerView.Controller = new RecyclerView.Controller()

  1. 设置列表整体效果类型 this.controller.setAdapterAnimation()
  2. 设置是否在item重复出现时显示动画效果 this.controller.setFirstOnly()
  3. 设置动画时长 this.controller.setDuration()

约束与限制

在下述版本验证通过:

  • DevEco Studio 版本: 4.1 Canary(4.1.3.317)
  • OpenHarmony SDK: API11 (4.1.0.36)

目录结构

|---- recyclerview_animators  
|     |---- entry  # 示例代码文件夹
|     |---- library  # 库文件夹
|	    |----src
          |----main
              |----ets
                  |----components
                      |----adapterAnimator #动画效果适配
                      |----itemAnimator #元素动画效果实现
                      |----RecyclerView.ets #核心类
|           |---- Index.ets  # 对外接口
|     |---- README.md  # 安装使用方法                    
如果在 Android `RecyclerView` 中使用默认的动画效果时出现锯齿,可以尝试以下解决方案: 1. 设置 `RecyclerView` 的 `clipToPadding` 属性为 `false`。 ```xml <androidx.recyclerview.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:clipToPadding="false" /> ``` 2. 在 `RecyclerView` 中使用 `ItemDecoration`。 ```kotlin class ItemDecoration : RecyclerView.ItemDecoration() { override fun getItemOffsets( outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State ) { outRect.set(0, 0, 0, 1) // 设置底部分割线,数值可以根据自己的需求调整 } override fun onDraw( canvas: Canvas, parent: RecyclerView, state: RecyclerView.State ) { super.onDraw(canvas, parent, state) val childCount = parent.childCount for (i in 0 until childCount) { val child = parent.getChildAt(i) val params = child.layoutParams as RecyclerView.LayoutParams val left = child.left + params.leftMargin val right = child.right + params.rightMargin val top = child.bottom + params.bottomMargin val bottom = top + 1 // 设置底部分割线,数值可以根据自己的需求调整 canvas.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), paint) } } private val paint = Paint().apply { color = Color.parseColor("#e5e5e5") // 分割线颜色 style = Paint.Style.FILL } } ``` 3. 使用第三方库 `RecyclerView Animators`。 ```gradle dependencies { implementation 'jp.wasabeef:recyclerview-animators:4.0.2' } ``` ```kotlin val animator = SlideInUpAnimator(OvershootInterpolator(1f)) recyclerView.itemAnimator = animator ``` 希望以上解决方案可以帮助到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值