在 ListView 中对列表项进行动画处理
我尝试在 ListView 中为新项目添加动画。我有稳定的 id-s,所以我确切地知道要为哪个元素设置动画。问题出在ListView的回收机制上。当我知道我最近插入了一个元素时,我会在视图上调用 startAnimation 。但随后,视图被回收,填充了不同的数据。 这会导致 UI 动画错误的行。在某些时候,视图持有正确的数据,但随后被回收。我通过 logcat 确认了这一点。 有什么办法可以解决这个问题吗?
编辑:
public ExpensCursorAdapter(Context context, Cursor c, boolean autoRequery,
CopyOnWriteArraySet<String> fadeAnimateTags) {
super(context, c, autoRequery);
this.mFadeAnimTags = fadeAnimateTags;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
setup(view, context, cursor);
}
private void setup(View view, Context context, Cursor cursor) {
final String id = cursor.getString(4);
if (LOCAL_LOGV) Log.v(TAG, String.format("Create item for %s. Received view: %s", id, view.toString()));
view.setTag(id);
final TextView dateText = (TextView) view.findViewById(R.id.date);
final TextView timeText = (TextView) view.findViewById(R.id.time);
final TextView title = (TextView) view.findViewById(R.id.title);
final TextView amount = (TextView) view.findViewById(R.id.amount);
final Date date = new Date(cursor.getLong(0));
title.setText(cursor.getString(1));
dateText.setText(dFormat.format(date));
timeText.setText(tFormat.format(date));
amount.setText(String.format("%d Ft", cursor.getInt(2)));
if (cursor.getInt(3) == 1) {
timeText.setTextColor(Color.LTGRAY);
title.setTextColor(Color.LTGRAY);
dateText.setTextColor(Color.LTGRAY);
amount.setTextColor(Color.LTGRAY);
} else {
timeText.setTextColor(Color.BLACK);
title.setTextColor(Color.BLACK);
dateText.setTextColor(Color.BLACK);
amount.setTextColor(Color.BLACK);
}
if (mFadeAnimTags.contains(id)) {
view.setAnimation(AnimationUtils.loadAnimation(context, R.anim.fade));
mFadeAnimTags.remove(id);
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.expense_list_item, parent, false);
setup(view, context, cursor);
return view;
}
I try to animate new items in my ListView. I have stable id-s, so I know exactly which element to animate. The problem comes from the recycle mechanism of ListView. I call startAnimation on the View when I know I got a recently inserted element. But then, the view got recycled, filled with different data.
It results on the UI animating the wrong row. At some point the view was holding the right data, but then got recycled. I confirmed this via logcat.
Is there any way to solve this?
EDIT:
public ExpensCursorAdapter(Context context, Cursor c, boolean autoRequery,
CopyOnWriteArraySet<String> fadeAnimateTags) {
super(context, c, autoRequery);
this.mFadeAnimTags = fadeAnimateTags;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
setup(view, context, cursor);
}
private void setup(View view, Context context, Cursor cursor) {
final String id = cursor.getString(4);
if (LOCAL_LOGV) Log.v(TAG, String.format("Create item for %s. Received view: %s", id, view.toString()));
view.setTag(id);
final TextView dateText = (TextView) view.findViewById(R.id.date);
final TextView timeText = (TextView) view.findViewById(R.id.time);
final TextView title = (TextView) view.findViewById(R.id.title);
final TextView amount = (TextView) view.findViewById(R.id.amount);
final Date date = new Date(cursor.getLong(0));
title.setText(cursor.getString(1));
dateText.setText(dFormat.format(date));
timeText.setText(tFormat.format(date));
amount.setText(String.format("%d Ft", cursor.getInt(2)));
if (cursor.getInt(3) == 1) {
timeText.setTextColor(Color.LTGRAY);
title.setTextColor(Color.LTGRAY);
dateText.setTextColor(Color.LTGRAY);
amount.setTextColor(Color.LTGRAY);
} else {
timeText.setTextColor(Color.BLACK);
title.setTextColor(Color.BLACK);
dateText.setTextColor(Color.BLACK);
amount.setTextColor(Color.BLACK);
}
if (mFadeAnimTags.contains(id)) {
view.setAnimation(AnimationUtils.loadAnimation(context, R.anim.fade));
mFadeAnimTags.remove(id);
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.expense_list_item, parent, false);
setup(view, context, cursor);
return view;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 http:// /kylewbanks.com/blog/Implementing-Google-Plus-Style-ListView-Animations-on-Android,
(无需自定义库)
If you use getView() simple code from http://kylewbanks.com/blog/Implementing-Google-Plus-Style-ListView-Animations-on-Android,
(No need custom library)
查看 ListViewAnimations 库来为 ListView 项目设置动画。
基本上,您只需要再添加两行即可使项目动画化:
之前:
之后:
Have a look at the ListViewAnimations library to animate your ListView items.
Basically, you would only have to add two more lines to have your items animated:
Before:
After:
我有类似的问题。我不确定这是否是正确的方法,但您可以尝试在开始新视图之前清除视图上的动画,以取消回收视图中的任何剩余动画。这对我有帮助。
I had a similar problem. I'm not 100% sure if this is the right way to go, but you can try clearing the animation on the view before starting a new one to cancel any leftover animation from the recycled view. This helped me.