1

我正在做一个简单的项目来查看动画的结果。我有一个 ListView,在单击按钮后,它将新项目添加到列表中,之后它应该为新项目设置动画,但它没有。这是一个代码:

package com.example.notifydatahaschanged;

     import java.util.ArrayList;  
    import java.util.List;  
    import android.app.Activity;  
    import android.os.Bundle;  
    import android.view.View;  
    import android.view.View.OnClickListener;  
    import android.view.animation.Animation;
    import android.view.animation.AnimationUtils;
    import android.view.animation.LayoutAnimationController;
    import android.widget.Button;  
    import android.widget.ListView;  

 public class MainActivity extends Activity implements OnClickListener {  

      private ListView mCompleteListView;  
      private Button mAddItemToList;  
      private List<String> mItems;  
      private CompleteListAdapter mListAdapter;  
      private static final int MIN = 0, MAX = 10000;  


      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_complete_list);  
           initViews();  
           mItems = new ArrayList<String>();  
           mListAdapter = new CompleteListAdapter(this, mItems);  
           mCompleteListView.setAdapter(mListAdapter);  
      }  
      private void initViews() {  
           mCompleteListView = (ListView) findViewById(R.id.completeList);  
           mAddItemToList = (Button) findViewById(R.id.addItemToList);  
           mAddItemToList.setOnClickListener(this);  
      }  
      private void addItemsToList() {  
           int randomVal = MIN + (int) (Math.random() * ((MAX - MIN) + 1));  
           mItems.add(0, String.valueOf(randomVal)); 
           Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), Animation.RELATIVE_TO_PARENT);
           animation.start();

           mListAdapter.notifyDataSetChanged();  
      }  

      @Override  
      public void onClick(View v) {  
           switch (v.getId()) {  
           case R.id.addItemToList:

                addItemsToList();  
                break;  
           }  
      }  
 }

CompleteListAdapter 类是下一个:

package com.example.notifydatahaschanged;


 import java.util.List;  
import android.app.Activity;  
import android.content.Context;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;  
import android.widget.TextView;  



 public class CompleteListAdapter extends BaseAdapter {  
      private Activity mContext;  
      private List<String> mList;  
      private LayoutInflater mLayoutInflater = null;  
      public CompleteListAdapter(Activity context, List<String> list) {  
           mContext = context;  
           mList = list;  
           mLayoutInflater = (LayoutInflater) mContext  
                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
      }  
      @Override  
      public int getCount() {  
           return mList.size();  
      }  
      @Override  
      public Object getItem(int pos) {  
           return mList.get(pos);  
      }  
      @Override  
      public long getItemId(int position) {  
           return position;  
      }  
      @Override  
      public View getView(int position, View convertView, ViewGroup parent) {  
           View v = convertView;  
           CompleteListViewHolder viewHolder;  
           if (convertView == null) {  
                LayoutInflater li = (LayoutInflater)  mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
                v = li.inflate(R.layout.list_layout, null);  
                viewHolder = new CompleteListViewHolder(v);  
                v.setTag(viewHolder);  
           } else {  
                viewHolder = (CompleteListViewHolder) v.getTag();  
           }  
           viewHolder.mTVItem.setText(mList.get(position));  

           return v;  
      }  
 }  
 class CompleteListViewHolder {  
      public TextView mTVItem;  
      public CompleteListViewHolder(View base) {  
           mTVItem = (TextView) base.findViewById(R.id.listTV);  
      }  
 } 

现在用于布局的 XML 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   tools:context=".CompleteListActivity" >  
   <ListView  
     android:id="@+id/completeList"  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"  
     android:layout_alignParentTop="true"  
     android:layout_marginBottom="80dp"
     android:layoutAnimation="@anim/anim" >  
   </ListView>  
   <Button  
     android:id="@+id/addItemToList"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:layout_alignParentBottom="true"  
     android:layout_margin="10dp"  
     android:text="Dodaj" />  

 </RelativeLayout>  

列表的下一个布局:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
           android:layout_width="match_parent"  
           android:layout_height="60dp"  
           android:orientation="vertical" >  
           <TextView  
             android:id="@+id/listTV"  
             android:layout_width="match_parent"  
             android:layout_height="60dp"  
             android:gravity="center_vertical"  
             android:textSize="42sp"  
             android:typeface="sans" />  
         </LinearLayout>  

现在还有一些动画 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
<scale
    android:fromXScale="0.5"
    android:toXScale="2.0"  
    android:startOffset="0"
    android:duration="300"
    android:fromYScale="0.5"
    android:toYScale="2.0"

    android:pivotX="10%"
    android:pivotY="60%"

    android:fillBefore="false" />
<rotate
           android:fromDegrees="0"
           android:toDegrees="360"
           android:toYScale="1.0"
           android:pivotX="60%"
           android:pivotY="60%"
           android:startOffset="300"
           android:duration="800" />
    </set>

最后一个动画文件:

<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
 android:delay="40%"
 android:animation="@anim/animation"
 />

一切正常,但只有第一个项目在添加到列表后才被动画化,仅此而已。请帮忙

4

0 回答 0