【问题标题】:Android ListView items animation - Animate only first visible itemsAndroid ListView 项目动画 - 仅动画第一个可见项目
【发布时间】:2015-02-01 08:48:50
【问题描述】:

我正在尝试重现此 ListView 项目:

http://material-design.storage.googleapis.com/videos/components-progressandactivity-progressandactivity-2-drive.mobile-load.folders_large_xhdpi.webm

我已经通过这些代码行成功实现了它(不包括动画 xml 文件):

//Set animation
    if(position > lastAnimPosition){
        lastAnimPosition = position;

        Animation anim = AnimationUtils.loadAnimation(context, R.anim.item_slide_in);
        anim.setStartOffset(50 * position);
        row.startAnimation(anim);
    }

所以动画很好,但是当我向下滚动时,偏移量有问题 - 它太大了,给人的感觉就像应用程序加载项目的时间过长。 我需要做的是找出我应该何时禁用偏移 - 或者如何仅为第一个显示的项目启用偏移。

【问题讨论】:

  • 能否请您发布 item_slide_in。
  • 能否请您发布您的 Item_slide_in 动画

标签: android listview animation offset


【解决方案1】:

在你的xml中使用布局动画属性

这里的文档:http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:layoutAnimation

将一个文件添加到您的 layout_item_slide_in.xml 的 res/anim 文件夹中,该文件引用了 @anim/item_slide_in

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
        android:delay="0.5"
        android:animation="@anim/item_slide_in" />

现在,稍微更改您的 listview xml 以包含 android:layoutAnimation 属性和值

<ListView
    android:id="@+id/foo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layoutAnimation="@anim/layout_item_slide_in"" />

这将实现您想要的相同的首次加载动画样式,而无需在代码中执行此操作

【讨论】:

  • 它就像一个魅力。但我有一个问题 - 有什么方法可以在 notifyDataSetChanged() 等事件上重新设置动画?
  • 尝试在adapter.notifyDataSetChanged() 调用之后在代码中的列表视图引用上调用mListView.startLayoutAnimation()
  • RecyclerView 也可以这样吗?
【解决方案2】:

YOUR_ADAPTER.notifyDataSetChanged(); 之前调用YOUR_LIST_VIEW.scheduleLayoutAnimation(); 以在dataSetChange 之后为您的列表重新设置动画

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    相关资源
    最近更新 更多