你的位置:首页 > 操作系统

[操作系统]Android ListView 删除动画


Android 的ListView在删除时,被删除的项目直接消失,比较生硬,在此实现一下删除动画,大家一起探讨;

  以下是Activity代码;主界面只有一个ListView,通过Adapter提供数据,界面如下,当点击某一项时,将播放动画,并删除点击的项目;

package app.com.listviewdeleteanimation;import android.animation.Animator;import android.animation.AnimatorSet;import android.animation.ObjectAnimator;import android.content.Context;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.view.animation.AccelerateDecelerateInterpolator;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;import java.util.ArrayList;public class MainActivity extends ActionBarActivity implements ListView.OnItemClickListener{
/**
* Adapter 提供数据,提供remove方法,可以删除指定的条目;
*
**/ public class Adapter extends BaseAdapter{ private Context mContext; private ArrayList<String> mItems; public Adapter(Context c,String [] data) { super(); mContext = c; mItems = new ArrayList<String>(); for (String item : data){ mItems.add(item); } } public void remove(int position){ if(position < mItems.size()){ mItems.remove(position); } notifyDataSetChanged(); } @Override public int getCount() { return mItems.size(); } @Override public int getItemViewType(int position) { // current menu type return position; } @Override public String getItem(int position) { return mItems.get(position); } @Override public boolean isEmpty() { return mItems.isEmpty(); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate( android.R.layout.simple_list_item_1, parent, false); } TextView tv = (TextView) convertView; tv.setText(getItem(position)); return convertView; } } private ListView mListView; private Adapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView)findViewById(R.id.listView); mAdapter = new Adapter(this, new String[] {"1","2","3","4","5","6","7",}); mListView.setAdapter(mAdapter); mListView.setOnItemClickListener(this); } @Override public void onItemClick(AdapterView<?> parent, View view, final int position, long id){
// 获得ListView第一个View的position int firstVisiblePosition = mListView.getFirstVisiblePosition();
// 存储所有的Animator,利用AnimatorSet直接播放 ArrayList<Animator> animators = new ArrayList<Animator>();
     // 获得要删除的View View itemToDelete = mListView.getChildAt(position - firstVisiblePosition); int viewHeight = itemToDelete.getHeight(); int dividerHeight = mListView.getDividerHeight(); ObjectAnimator hideAnimator = ObjectAnimator.ofFloat(itemToDelete, "alpha",1f, 0f); animators.add(hideAnimator); int delay = 0; int firstViewToMove = position + 1; for (int i=firstViewToMove;i < mListView.getChildCount(); ++i){ View viewToMove = mListView.getChildAt(i); ObjectAnimator moveAnimator = ObjectAnimator.ofFloat(viewToMove, "translationY", 0, -dividerHeight-viewHeight); moveAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); moveAnimator.setStartDelay(delay); delay += 100; animators.add(moveAnimator); } AnimatorSet set = new AnimatorSet(); set.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) {} @Override public void onAnimationEnd(Animator animation) { mAdapter.remove(position); // 动画结束后,恢复ListView所有子View的属性 for (int i=0;i<mListView.getChildCount();++i){ View v = mListView.getChildAt(i); v.setAlpha(1f); v.setTranslationY(0); } } @Override public void onAnimationCancel(Animator animation) {} @Override public void onAnimationRepeat(Animator animation) {} }); set.playTogether(animators); set.start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.