将动画添加到Android中的列表视图

ASH*_*ASH 10 android android-animation

我想为列表视图的项目设置动画.目前,无论何时添加新项目,我都会在列表项上应用过渡动画.但这不是我想要实现的动画.我希望当列表视图中添加新项目时,整个列表视图会向下移动一个位置以便为新添加的项目腾出空间.

目前我使用的代码是:

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);
Run Code Online (Sandbox Code Playgroud)

然后通过按钮onClick添加项目

    l.startLayoutAnimation();
Run Code Online (Sandbox Code Playgroud)

任何其他建议来实现这样的动画.

ASH*_*ASH 14

我得到了解决方案.我在自定义适配器的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;
    }
Run Code Online (Sandbox Code Playgroud)

从而实现了我所说的动画.

  • 什么是'旗帜'? (8认同)