ListView 实现带有Filpper效果的左右滑动删除 Item 的实现最主要的方法还是 对 Listview 的继承重写 。然后是在删除过程中添加 TranslateAnimation 滑动事件。
<span style="font-size:14px;">public class FilpperActivity extends Activity { private FilpperListvew flipperListView; private MyAdapter adapter ; private List<String> items; private int width; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_filpper); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); width = dm.widthPixels; flipperListView = (FilpperListvew) findViewById(R.id.filpperlistview); items = new ArrayList<String>(); for(int i=0;i<15;i++){ items.add("item---->"+i); } adapter = new MyAdapter(this, items); flipperListView.setAdapter(adapter); //自定义的接口 flipperListView.setFilpperDeleteListener(new FilpperDeleteListener() { @Override public void filpperDelete(float xPosition,float yPosition) { //listview中要有item,否则返回 if(flipperListView.getChildCount() == 0) return ; //根据坐标获得滑动删除的item的index final int index = flipperListView.pointToPosition((int)xPosition, (int)yPosition); //一下两步是获得该条目在屏幕显示中的相对位置,直接根据index删除会空指針异常。因为listview中的child只有当前在屏幕中显示的才不会为空 int firstVisiblePostion = flipperListView.getFirstVisiblePosition(); View view = flipperListView.getChildAt(index - firstVisiblePostion); <span style="color:#ff0000;">TranslateAnimation tranAnimation = new TranslateAnimation(0,width,0,0); tranAnimation.setDuration(500); tranAnimation.setFillAfter(true); view.startAnimation(tranAnimation);</span> //当动画播放完毕后,删除。否则不会出现动画效果(自己试验的)。 tranAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { //删除一个item items.remove(index); adapter.notifyDataSetChanged(); } }); } }); } } </span>
<span style="font-size:14px;">package com.example.filpperdeletelist; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.ListView; public class FilpperListvew extends ListView { private float myLastX = -1; private float myLastY = -1; private boolean delete = false; //自定义的滑动删除监听 private FilpperDeleteListener filpperDeleterListener; public FilpperListvew(Context context) { super(context); // TODO Auto-generated constructor stub } public FilpperListvew(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override public boolean onTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: // 获得第一个点的x坐标 myLastX = ev.getX(0); myLastY = ev.getY(0); break; case MotionEvent.ACTION_MOVE: // 得到最后一个点的坐标 float deltaX = ev.getX(ev.getPointerCount() - 1) - myLastX; float deltaY = Math.abs(ev.getY(ev.getPointerCount() - 1) - myLastY); // 可以滑动删除的条件:横向滑动大于100,竖直差小于50 if (deltaX > 100.0 && deltaY < 50) { delete = true; } break; case MotionEvent.ACTION_UP: if (delete && filpperDeleterListener != null) { filpperDeleterListener.filpperDelete(myLastX,myLastY); } reset(); break; } return super.onTouchEvent(ev); } public void reset(){ delete = false ; myLastX = -1 ; myLastY = -1 ; } public void setFilpperDeleteListener(FilpperDeleteListener f) { filpperDeleterListener = f; } //自定义的接口 public interface FilpperDeleteListener { public void filpperDelete(float xPosition,float yPosition); } } </span>
<span style="font-size:14px;">package com.example.filpperdeletelist; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class MyAdapter extends BaseAdapter { private List<String> list ; private Context context ; public MyAdapter(Context context,List<String> list){ this.context = context ; this.list = list; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub convertView = LayoutInflater.from(context).inflate(R.layout.lv_item, null); TextView text = (TextView)convertView.findViewById(R.id.tv); text.setText(list.get(position)); return convertView; } } </span>
下载地址:
http://download.csdn.net/detail/jia635/7651981
ListView 实现带有Filpper效果的左右滑动删除 Item
原文地址:http://blog.csdn.net/jia635/article/details/37955599