我正在做一个基于 ListView 的小型 Android 应用程序。当用户在列表中选择一个或多个元素并随后从 ActionBar 中选择一个菜单项时,我想对列表中的选定元素做一个小动画,这就是问题所在。
没有任何东西有生气——也没有任何东西失败。以下代码片段是我正在做的事情的简化版本:
private void animateListViewItem()
{
TranslateAnimation anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f);
anim.setDuration(2000);
View v = fragment.getListAdapter().getView(fragment.getListView().getFirstVisiblePosition(), null, null);
v.startAnimation(anim);
}
当我弄乱它,试图找出问题所在时,我曾一度用整个 ListView 替换该项目,以排除动画是问题的根源——就像这样。
private void animateListViewItem()
{
TranslateAnimation anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f);
anim.setDuration(2000);
fragment.getListView().startAnimation(anim);
}
令我惊讶的是,这很完美!
所以我的问题是 - 为什么我不能为 ListView 中的各个元素设置动画?还是我做错了什么?
谢谢!
PS 作为记录,ListView 填充了自定义视图(LinearLayouts),并且我在制作动画之前检查了我是否获得了正确的项目。