首页 文章

在ListView中为列表项设置动画

提问于
浏览
6

我尝试在ListView中为新项目制作动画 . 我有稳定的id-s,所以我确切知道要动画的元素 . 问题来自ListView的循环机制 . 当我知道我有一个最近插入的元素时,我在视图上调用startAnimation . 但随后,视图得到了回收,充满了不同的数据 . 它会在UI上为错误的行设置动画 . 在某些时候,视图持有正确的数据,但随后被回收 . 我通过logcat证实了这一点 . 有什么方法可以解决这个问题吗?

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;
}

4 回答

  • 5

    在自定义适配器的getView方法中为每个添加的元素设置动画 .

    public View getView(int position, View convertView, ViewGroup parent) {
    
        View v = convertView;
    
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.simple_list_item_1, null);
        }
    
        ListData o = list.get(position);
        TextView tt = (TextView) v.findViewById(R.id.toptext);
    
        tt.setText(o.content);
    
        Log.d("ListTest", "Position : "+position);
    
        if(flag == false) {
            Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_top_to_bottom);
            v.startAnimation(animation);
        }
        return v;
    }
    

    从而实现动画 .

  • 1

    如果你在这里使用getView()简单代码, (No need custom library)

    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

  • 4

    查看ListViewAnimations库以设置ListView项目的动画 .

    基本上,您只需添加两行即可为您的项目设置动画:

    之前:

    MyListAdapter mAdapter = new MyListAdapter(this, getItems());
    getListView().setAdapter(mAdapter);
    

    后:

    MyListAdapter mAdapter = new MyListAdapter(this, getItems());
    AlphaInAnimationAdapter alphaInAnimationAdapter = new AlphaInAnimationAdapter(mAdapter);
    alphaInAnimationAdapter.setAbsListView(getListView());
    getListView().setAdapter(alphaInAnimationAdapter);
    
  • 3

    我遇到了类似的问题 . 我不是100%确定这是否是正确的方法,但你可以尝试清除视图上的动画,然后再开始一个新的动画从取回的视图中取消任何剩余的动画 . 这对我有帮助 .

    if(flag == false) {
        v.clearAnimation();
        Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_top_to_bottom);
        v.startAnimation(animation);
    }
    

相关问题