我正在做一个基于 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 中的各个元素设置动画?或者我做错了什么?
Thanks!
附:根据记录,ListView 填充了自定义视图(LinearLayouts),并且我已检查在制作动画之前是否获得了正确的项目。