2012-01-17 63 views
10

我想动画列表视图的项目。目前,我正在应用添加新项目时列表项目上的Transition Animation。但这不是我想要实现的动画。我希望在列表视图中添加新项目时,整个列表视图会移动一个位置,让新添加的项目让位。将动画添加到Android中的列表视图

目前我使用的代码是:

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

然后同时通过按钮添加项目的onClick

l.startLayoutAnimation(); 

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

回答

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

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

+0

我在'R.anim.slide_top_to_bottom'找到错误anim无法解析或不是字段 – Nishant 2012-05-24 05:52:56

+0

您是否在文件夹中添加xml? – ASH 2012-05-24 06:27:17

+0

不可以你给那个文件的代码吗? – Nishant 2012-05-24 07:06:20