2012-01-17 23 views
10

リストビューのアイテムをアニメーション化したいと思います。現在私は、新しい項目が追加されるたびにリスト項目にトランジションアニメーションを適用しています。しかし、これは私が達成したいアニメーションではありません。私はその時点でリストビューに新しいアイテムが追加されると、リストビュー全体が新たに追加されたアイテムのために場所を下に移動することを望んでいます。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); 

そして

l.startLayoutAnimation(); 

のonClick

ボタンから、このようなアニメーションを実現するために、任意の他の提案を項目を追加しています。

答えて

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

+0

xmlをanimフォルダに追加しましたか? – ASH

+0

あなたはそのファイルのコードを提供できませんか? – Nishant

関連する問題