17

我正在使用一组数据(ArrayList)在 Android 上填充ListView. 在我更改排序模式的那一刻,我重新排序链接到适配器的数据并调用notifyOnDataChanged()以更新列表。

这可以完成工作,但我希望能够将每个项目动画到其新位置(如在 iOS 上),因为两个数据集包含相同的项目 - 只是重新排序。

我花了一些时间思考如何为它设置动画,并决定将所有出现在列表下方的项目设置为高度为零,然后在新位置重新设置为正常大小。这将使所有在数据中向上移动的项目实际上在列表中向上移动,因为项目在其上方消失。这就是想法,但我无法弄清楚如何实现它。

数据可以这样表示:

Set A | index | Set B
------+-------+------
A     | 0     | B
B     | 1     | A
C     | 2     | C
D     | 3     | F
E     | 4     | G
F     | 5     | E
G     | 6     | D

我只是在寻找 iOS 默认为 Android 处理的东西。不应该这么难。

4

2 回答 2

11

做了一个小例子,动画这样一个列表的重新排序。动画与您描述的不完全一样,但希望它可以帮助您实现您想要的。

https://github.com/fabrantes/widgetexamples

流程是这样的:

  1. 在 notifyDataSetChanged() 之前保存当前状态。保存状态包括为每个项目存储 getTop() 坐标。
  2. 在 notifyDataSetChange 之后,遍历所有 ListView 视图并应用 TranslateAnimation 与新项目位置和旧位置之间的高度差。
于 2013-03-01T15:17:49.870 回答
3

我希望这是您的问题的解决方案

  <ListView
            android:id="@+id/listUniversal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layoutAnimation="@anim/controller">

        </ListView>

动画.xml

<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/scale_1"
    android:delay="50%" />

scale_1.xml

<scale
    android:duration="200"
    android:fromXScale="0.1"
    android:fromYScale="0.1"
    android:pivotX="10%"
    android:pivotY="10%"
    android:startOffset="100"
    android:toXScale="1"
    android:toYScale="1.0" />

于 2013-03-04T14:13:57.627 回答