如何在ListView中各个元素动画 [英] How to animate individual elements in ListView
问题描述
我在做围绕一个ListView一个小的Android应用程序。当用户选择列表中的一个或多个元素,并随后选择从动作条的菜单项我愿做一个小动画在列表中选定的元素,而这正是出问题。
I'm doing a small Android app based around a ListView. When the user selects one or more elements in the list and subsequently selects a menu item from the ActionBar I would like to do a small animation on the selected elements in the list, and this is where things go wrong.
没有动画 - 也没有什么失败。下面code片是我在做什么的简化版本:
Nothing animates - nor does anything fail. The following code piece is a simplified version of what I'm doing:
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排除动画作为问题的根源 - 这样
When I messed around with it, trying to figure out what was wrong, I at one point substituted the item with the entire ListView to rule out the animation as the source of the problem - like this.
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);
}
要我吃惊的是,该工作完美!
To my amazement, that worked perfect!
所以我的问题是 - 为什么我不能动画在ListView的各个元素?或者是有什么我做错了吗?
So my question is - why can't I animate the individual elements in a ListView? Or is there something I am doing wrong?
谢谢!
P.S。备案的ListView填充了自定义视图(LinearLayouts),我也检查了我以前动画得到正确的项目。
P.S. For the record the ListView is populated with custom views (LinearLayouts), and I have checked that I get the right item before animating.
推荐答案
我发现了什么的问题是:
I found out what the issue was:
View v = fragment.getListAdapter().getView(fragment.getListView().getFirstVisiblePosition(), null, null);
此行是问题。它返回的新的查看
在列表中没有的现有的<$ c中的指定位置,显示基础数据$ C>查看。所以返回的查看
无关的列表中。
This line was the problem. It returns a new View
to display the underlying data at the specified position in the list not the existing View
. So the returned View
has nothing to do with the list.
相反,这样做的:
View v = fragment.getListView().getChildAt(fragment.getListView().getFirstVisiblePosition());
给我找了查看
的列表中使用和动画工作正常。
Got me the View
that the list was using and the animation worked as expected.
这篇关于如何在ListView中各个元素动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!