gpt4 book ai didi

android - 将动画添加到 Android 中的 ListView

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

我想为 ListView 的项目设置动画。目前,每当添加新项目时,我都会在列表项目上应用过渡动画。但这不是我想要实现的动画。我希望当时在 ListView 中添加新项目时,整个 ListView 向下移动一个位置以为新添加的项目让路。

目前我使用的代码是:

set = new AnimationSet(true);

animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(50);
set.addAnimation(animation);

animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(150);
set.addAnimation(animation);

LayoutAnimationController controller = new LayoutAnimationController(set, 1.0f);
l.setLayoutAnimation(controller);
l.setAdapter(listAdaptor);

然后通过点击按钮添加项目

    l.startLayoutAnimation();

实现此类动画的任何其他建议。

最佳答案

我得到了解决方案。我在自定义适配器的 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;
}

从而实现了我所说的动画效果。

关于android - 将动画添加到 Android 中的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8893503/

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