领导看到iphone上tableview有个滑动删除的效果,要求在android上也实现,搜了下资料,实现起来比较简单,可弄到后面,居然不能点击了,把一篇文章中的代码修改了一下,捣鼓了一番,搞定,下面是代码:
1 package com.example.listsildedel; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import android.app.Activity; 7 import android.content.Intent; 8 import android.os.Bundle; 9 import android.widget.ListView;10 11 public class MainActivity extends Activity {12 13private ListView mListView;14@Override15public void onCreate(Bundle savedInstanceState) {16 super.onCreate(savedInstanceState);17 setContentView(R.layout.activity_main);18 mListView = (ListView) this.findViewById(R.id.list);19 List<String> list = new ArrayList<String>();20 for(int i=0;i<10;i++){21 list.add("选项"+i);22 }23 //实例化自定义内容适配类24 MyAdapter adapter = new MyAdapter(this, this,list);25 //为listView设置适配26 mListView.setAdapter(adapter);27}28 29public void openActivity(){30 Intent intent = new Intent(this, TestActivity.class);31 startActivity(intent);32}3334 }
adapter:
1 package com.example.listsildedel; 2 3 import java.util.List; 4 5 import android.content.Context; 6 import android.view.LayoutInflater; 7 import android.view.MotionEvent; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.view.View.OnTouchListener; 11 import android.view.ViewGroup; 12 import android.widget.BaseAdapter; 13 import android.widget.Button; 14 import android.widget.TextView; 15 16 public class MyAdapter extends BaseAdapter { 17 18private List<String> arrays = null; 19private Context mContext; 20private Button curDel_btn; 21private float x,ux; 22private MainActivity mActivity; 2324public MyAdapter(MainActivity activity, Context mContext, List<String> arrays) { 25 this.mContext = mContext; 26 this.arrays = arrays; 27 mActivity = activity; 28 29} 30 31public int getCount() { 32 return this.arrays.size(); 33} 34 35public Object getItem(int position) { 36 return null; 37} 38 39public long getItemId(int position) { 40 return position; 41} 42 43public View getView(final int position, View view, ViewGroup arg2) { 44 ViewHolder viewHolder = null; 45 if (view == null) { 46 viewHolder = new ViewHolder(); 47 view = LayoutInflater.from(mContext).inflate(R.layout.item, null); 48 viewHolder.tvTitle = (TextView) view.findViewById(R.id.title); 49 viewHolder.btnDel = (Button) view.findViewById(R.id.del); 50 view.setTag(viewHolder); 51 } else { 52 viewHolder = (ViewHolder) view.getTag(); 53 } 54 //换掉了原来listview中的onItemClick 55 <span style="font-size:18px;"><strong>view.setOnClickListener(new OnClickListener() { 56 57 public void onClick(View v) { 58 // TODO Auto-generated method stub 59 mActivity.openActivity(); 60 } 61 });</strong></span> 62 63 64 //为每一个view项设置触控监听 65 view.setOnTouchListener(new OnTouchListener() { 66 public boolean onTouch(View v, MotionEvent event) { 67 68 final ViewHolder holder = (ViewHolder) v.getTag(); 69 70 //当按下时处理 71 if (event.getAction() == MotionEvent.ACTION_DOWN) { 72 73 ////设置背景为选中状态 74 //v.setBackgroundResource(R.drawable.mm_listitem_pressed); 75 //获取按下时的x轴坐标 76 x = event.getX(); 77 //判断之前是否出现了删除按钮如果存在就隐藏 78 if (curDel_btn != null) { 79if(curDel_btn.getVisibility() == View.VISIBLE){ 80 curDel_btn.setVisibility(View.GONE); 81 return true; 82} 83 } 84 85 } else if (event.getAction() == MotionEvent.ACTION_UP) {// 松开处理 86 87 //设置背景为未选中正常状态 88 //v.setBackgroundResource(R.drawable.mm_listitem_simple); 89 //获取松开时的x坐标 90 ux = event.getX(); 91 92 //判断当前项中按钮控件不为空时 93 if (holder.btnDel != null) { 9495//按下和松开绝对值差当大于20时显示删除按钮,否则不显示 9697if (Math.abs(x - ux) > 20) { 98 holder.btnDel.setVisibility(View.VISIBLE); 99 curDel_btn = holder.btnDel;100 return true;101}102 }103 } else if (event.getAction() == MotionEvent.ACTION_MOVE) {//当滑动时背景为选中状态104 return true;105 //v.setBackgroundResource(R.drawable.mm_listitem_pressed);106 107 108 } else {//其他模式109 //设置背景为未选中正常状态110 //v.setBackgroundResource(R.drawable.mm_listitem_simple);111 112 }113 114 return false;115 }116 });117 viewHolder.tvTitle.setText(this.arrays.get(position));118 119 //为删除按钮添加监听事件,实现点击删除按钮时删除该项120 viewHolder.btnDel.setOnClickListener(new OnClickListener() {121 122 public void onClick(View v) {123 if(curDel_btn!=null)124 curDel_btn.setVisibility(View.GONE);125 arrays.remove(position);126 notifyDataSetChanged();127 128 }129 });130 return view;131 132}133 134final static class ViewHolder {135 TextView tvTitle;136 Button btnDel;137}138 }
代码下载: /detail/wulingbo/6691885
转载请注明出处