对 ListView 中新添加的项目进行动画处理

发布于 2024-12-27 13:14:23 字数 141 浏览 3 评论 0原文

如何为 ListView 中新添加的项目添加动画?

我有一个适配器,当我在列表中添加新项目时,我说adapter.notifyDataSetChanged();项目已添加,一切正常,但我的问题是我想要新添加的元素具有一些动画。

How can I animate newly added items in ListView?

I have a adapter and when I add new items in my list I say adapter.notifyDataSetChanged(); the items are added, everything works perfectly, but my problem is I want newly added element to have some animation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

囚我心虐我身 2025-01-03 13:14:23

自定义适配器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;
}

从而实现动画

Animate each added element in the getView() method of your Custom Adapter.

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

And thereby achieve the Animation.

如若梦似彩虹 2025-01-03 13:14:23

关于 Android 动画的官方文档说,您可以使用 android:animateLayoutChanges="true" 设置动画在布局更改时触发。

摘自:http://developer.android.com/training/animation/layout.html

The official docs about animation in Android say that you can set an animation to trigger whenever the layout is changed, using android:animateLayoutChanges="true".

Taken from: http://developer.android.com/training/animation/layout.html

土豪我们做朋友吧 2025-01-03 13:14:23

添加这种动画比我最初想象的要困难。根据您想要实现的动画类型,有两种方法。

这确实是一个黑客,但我发现向ListView添加动画的唯一方法< /code> 的子项如下:

您可以尝试通知适配器您想要删除的项目的 id,并调用 adapter.notifyDataSetChanged();。这将生成对适配器的 getView() 方法的调用。在其中您可以执行以下操作:

if ( item.getId() == itemToRemove ) {
 //apply the animation
}

动画完成后,您可以调用 adapter.notifyDataSetChanged() 将所有内容放置到位。

Adding this kind of animations is harder than I first thought of. There are two ways depending of the kind of animation you are trying to achieve.

This is quite a hack but the only way I found to add an animation to ListView's children is the following:

You can try notifying the adapter the id of the item you are willing to delete and call adapter.notifyDataSetChanged();. This will generate calls to the adapter's getView() method. Inside it you can do something like:

if ( item.getId() == itemToRemove ) {
 //apply the animation
}

After the animation finished you can recall adapter.notifyDataSetChanged() to put everything in place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文