gpt4 book ai didi

android - ListView 中的动画列表项

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:57:51 25 4
gpt4 key购买 nike

我尝试为我的 ListView 中的新项目设置动画。我有稳定的 id-s,所以我确切地知道要为哪个元素设置动画。问题出在ListView的回收机制上。当我知道我有一个最近插入的元素时,我在 View 上调用 startAnimation。但是随后, View 被回收,充满了不同的数据。它会导致 UI 为错误的行设置动画。在某些时候, View 持有正确的数据,但随后被回收。我通过 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;
}

最佳答案

如果你在这里使用 getView() 简单的代码,(无需自定义库)

private int lastPosition = -1;

@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Load your view, populate it, etc...
View view = ...;

Animation animation = AnimationUtils.loadAnimation(getContext(), (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
view.startAnimation(animation);
lastPosition = position;

return view;
}

up_from_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="@android:anim/decelerate_interpolator">
<translate
android:fromXDelta="0%" android:toXDelta="0%"
android:fromYDelta="100%" android:toYDelta="0%"
android:duration="400" />
</set>

down_from_bottom.xml

 <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="@android:anim/decelerate_interpolator">
<translate
android:fromXDelta="0%" android:toXDelta="0%"
android:fromYDelta="-100%" android:toYDelta="0%"
android:duration="400" />
</set>

来自本网站的引用:http://kylewbanks.com/blog/Implementing-Google-Plus-Style-ListView-Animations-on-Android

关于android - ListView 中的动画列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7055470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com