ListView中item删除动画效果

本文介绍如何在Android的ListView中实现删除项的动画效果。通过自定义适配器和动画监听器,可以平滑地移除列表项,提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在android中listview控件使用的比较多,如果在listview上面有些动画会增色不少,listview上面的动画确实也不少,好多动画基本都是国外的大神写的。

有个github的下载地址里面包含了各种各样的listview动画,地址:https://github.com/nhaarman/ListViewAnimations

我这个也是看了别人写的,摘抄了下。因为自己也是个菜鸟。

首先定义一个listview中适配器一行的布局:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="horizontal" >  
  6.   
  7.     <ImageButton  
  8.         android:id="@+id/cell_trash_button"  
  9.         android:layout_width="48dp"  
  10.         android:layout_height="48dp"  
  11.         android:src="@drawable/trash_can"  
  12.         android:scaleType="fitXY" />  
  13.       
  14.     <TextView  
  15.         android:id="@+id/cell_name_textview"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_gravity="center_vertical|left"  
  19.         android:layout_marginLeft="16dp"  
  20.         android:layout_weight="1"  
  21.         android:text="cell name" />  
  22.       
  23. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

	<ImageButton
	    android:id="@+id/cell_trash_button"
	    android:layout_width="48dp"
	    android:layout_height="48dp"
	    android:src="@drawable/trash_can"
	    android:scaleType="fitXY" />
	
	<TextView
	    android:id="@+id/cell_name_textview"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_gravity="center_vertical|left"
	    android:layout_marginLeft="16dp"
	    android:layout_weight="1"
	    android:text="cell name" />
	
</LinearLayout>
然后定义一个适配器对象ArrayAdapter:
  1. public class MyAnimListAdapter extends ArrayAdapter<MyCell> {  
  2.     private LayoutInflater mInflater;  
  3.     private int resId;  
  4.   
  5.     public MyAnimListAdapter(Context context, int textViewResourceId, List<MyCell> objects) {  
  6.         super(context, textViewResourceId, objects);  
  7.         this.resId = textViewResourceId;  
  8.         this.mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  9.     }  
  10.   
  11.     @Override  
  12.     public View getView(final int position, View convertView, ViewGroup parent) {  
  13.         final View view;  
  14.         ViewHolder vh;  
  15.         MyCell cell = (MyCell)getItem(position);  
  16.   
  17.         if (convertView==null) {  
  18.             view = mInflater.inflate(R.layout.chain_cell, parent, false);  
  19.             setViewHolder(view);  
  20.         }  //理解:例如总共5个item,我们移除第四个item,当再次走到这里时候,第四条记录是原来的第五条记录,
  21.           //而现在用的是原来第四条记录的convertView
  22.          //但是我们在移除第四条记录做动画效果时,对第四条记录的view(指向convertView)的布局做了破坏
  23.        //所以此时使用第四条记录的convertView是不合适的,所以我们破坏了convertView的同时并记录了neewInflate是true
  24.     //当我们再次遇到此种被破坏的convertView的时候,我们从重新Inflate
  25.         else if (((ViewHolder)convertView.getTag()).needInflate) {  
  26.             view = mInflater.inflate(R.layout.chain_cell, parent, false);  
  27.             setViewHolder(view);  
  28.         }  
  29.         else {  
  30.             view = convertView;  
  31.         }  
  32.   
  33.         vh = (ViewHolder)view.getTag();  
  34.         vh.text.setText(cell.name);  
  35.         vh.imageButton.setOnClickListener(new OnClickListener() {  
  36.             @Override  
  37.             public void onClick(View v) {  
  38.                 deleteCell(view, position);  
  39.             }  
  40.         });  
  41.   
  42.         return view;  
  43.     }  
  44.   
  45.     private void setViewHolder(View view) {  
  46.         ViewHolder vh = new ViewHolder();  
  47.         vh.text = (TextView)view.findViewById(R.id.cell_name_textview);  
  48.         vh.imageButton = (ImageButton) view.findViewById(R.id.cell_trash_button);  
  49.         vh.needInflate = false;  
  50.         view.setTag(vh);  
  51.     }  
  52. }  
	public class MyAnimListAdapter extends ArrayAdapter<MyCell> {
		private LayoutInflater mInflater;
		private int resId;

		public MyAnimListAdapter(Context context, int textViewResourceId, List<MyCell> objects) {
			super(context, textViewResourceId, objects);
			this.resId = textViewResourceId;
			this.mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			final View view;
			ViewHolder vh;
			MyCell cell = (MyCell)getItem(position);

			if (convertView==null) {
				view = mInflater.inflate(R.layout.chain_cell, parent, false);
				setViewHolder(view);
			}
			else if (((ViewHolder)convertView.getTag()).needInflate) {
				view = mInflater.inflate(R.layout.chain_cell, parent, false);
				setViewHolder(view);
			}
			else {
				view = convertView;
			}

			vh = (ViewHolder)view.getTag();
			vh.text.setText(cell.name);
			vh.imageButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					deleteCell(view, position);
				}
			});

			return view;
		}

		private void setViewHolder(View view) {
			ViewHolder vh = new ViewHolder();
			vh.text = (TextView)view.findViewById(R.id.cell_name_textview);
			vh.imageButton = (ImageButton) view.findViewById(R.id.cell_trash_button);
			vh.needInflate = false;
			view.setTag(vh);
		}
	}
然后定义个动画监听和动画对象:
  1. private void deleteCell(final View v, final int index) {  
  2.     AnimationListener al = new AnimationListener() {  
  3.         @Override  
  4.         public void onAnimationEnd(Animation arg0) {  
  5.             mAnimList.remove(index);  
  6.   
  7.             ViewHolder vh = (ViewHolder)v.getTag();  
  8.             vh.needInflate = true;  
  9.   
  10.             mMyAnimListAdapter.notifyDataSetChanged();  
  11.         }  
  12.         @Override public void onAnimationRepeat(Animation animation) {}  
  13.         @Override public void onAnimationStart(Animation animation) {}  
  14.     };  
  15.   
  16.     collapse(v, al);  
  17. }  
  18.   
  19. private void collapse(final View v, AnimationListener al) {  
  20.     final int initialHeight = v.getMeasuredHeight();  
  21.   
  22.     Animation anim = new Animation() {  
  23.         @Override  
  24.         protected void applyTransformation(float interpolatedTime, Transformation t) {  
  25.             if (interpolatedTime == 1) {  
  26.                 v.setVisibility(View.GONE);  
  27.             }  
  28.             else {  
  29.                 v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);  
  30.                 v.requestLayout();  
  31.             }  
  32.         }  
  33.   
  34.         @Override  
  35.         public boolean willChangeBounds() {  
  36.             return true;  
  37.         }  
  38.     };  
  39.   
  40.     if (al!=null) {  
  41.         anim.setAnimationListener(al);  
  42.     }  
  43.     anim.setDuration(ANIMATION_DURATION);  
  44.     v.startAnimation(anim);  
  45. }  
	private void deleteCell(final View v, final int index) {
		AnimationListener al = new AnimationListener() {
			@Override
			public void onAnimationEnd(Animation arg0) {
				mAnimList.remove(index);

				ViewHolder vh = (ViewHolder)v.getTag();
				vh.needInflate = true;

				mMyAnimListAdapter.notifyDataSetChanged();
			}
			@Override public void onAnimationRepeat(Animation animation) {}
			@Override public void onAnimationStart(Animation animation) {}
		};

		collapse(v, al);
	}

	private void collapse(final View v, AnimationListener al) {
		final int initialHeight = v.getMeasuredHeight();

		Animation anim = new Animation() {
			@Override
			protected void applyTransformation(float interpolatedTime, Transformation t) {
				if (interpolatedTime == 1) {
					v.setVisibility(View.GONE);
				}
				else {
					v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
					v.requestLayout();
				}
			}

			@Override
			public boolean willChangeBounds() {
				return true;
			}
		};

		if (al!=null) {
			anim.setAnimationListener(al);
		}
		anim.setDuration(ANIMATION_DURATION);
		v.startAnimation(anim);
	}
上面主要是getview中调用删除每一个item的动画实现,后面随便定义一个listview对象和数据源把这个listview显示出来就行看到效果了。

代码来自:https://github.com/paraches/ListViewCellDeleteAnimation

http://blog.youkuaiyun.com/lonely_fireworks/article/details/25777497

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值