我嘗試在ListView中爲新項目添加動畫。我有穩定的id-s,所以我確切知道哪個元素需要動畫。問題來自ListView的回收機制。當我知道我有最近插入的元素時,我在視圖上調用startAnimation。但隨後,視圖得到了回收,充滿了不同的數據。 結果在用戶界面動畫錯誤的行。在某個時候,該觀點持有正確的數據,但隨後被回收。我通過logcat證實了這一點。 有什麼辦法可以解決這個問題嗎?在ListView中動畫列表項目
編輯:
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;
}
你在哪裏開始的動畫?我認爲它應該在Adapter的getView()方法上。 – manelizzard
我確實從那裏開始。代碼添加到問題中。 – gmate
每次「設置」行時,您是否嘗試過創建新的動畫對象?也許因爲你使用的是靜態方法,它總是引用相同的動畫,並且它從一行變到另一行。 – manelizzard