转自http://104zz.iteye.com/blog/1682431
本例满足一些开发者为了实现像iphone中list选项的滑动删除功能,在android中没有现成的控件可以实现这个功能,所以研究了下在自定义的适配类中为每个项的contentview设置触控监听OnTouchListener实现,具体实现步骤如下。
第一步:设计main.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" >
- <ListView
- android:id="@+id/list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:divider="@null" />
- </RelativeLayout>
第二步:设计item.xml添加一个TextView和一个Button 如下:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/contact_listitem"
- android:orientation="horizontal"
- android:gravity="center_vertical"
- >
- <TextView
- android:id="@+id/title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:gravity="center_vertical"
- android:layout_gravity="center_vertical"
- android:paddingTop="8dip"
- android:paddingLeft="8dip"
- android:paddingBottom="8dip"
- android:singleLine="true"
- android:text="hhhh"
- android:layout_margin="8dip"
- />
- <Button
- android:id="@+id/del"
- android:layout_width="wrap_content"
- android:singleLine="true"
- android:layout_height="wrap_content"
- android:gravity="center_vertical"
- android:layout_gravity="center_vertical"
- android:layout_alignParentRight="true"
- android:paddingLeft="8dip"
- android:paddingRight="8dip"
- android:textColor="#ffffff"
- android:background="@drawable/btn_style_six_normal"
- android:text="删除"
- android:layout_margin="8dip"
- android:visibility="gone"
- />
- </RelativeLayout>
第三步:创建设配类MyAdapter继承BaseAdapter在本类中主要是对触控滑动显示删除按钮做了比较详细,其他的跟一般的listview设计大同小异,代码如下:
- import java.util.List;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.View.OnTouchListener;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.Button;
- import android.widget.TextView;
- public class MyAdapter extends BaseAdapter {
- private List<String> arrays = null;
- private Context mContext;
- private Button curDel_btn;
- private float x,ux;
- public MyAdapter(Context mContext, List<String> arrays) {
- this.mContext = mContext;
- this.arrays = arrays;
- }
- public int getCount() {
- return this.arrays.size();
- }
- public Object getItem(int position) {
- return null;
- }
- public long getItemId(int position) {
- return position;
- }
- public View getView(final int position, View view, ViewGroup arg2) {
- ViewHolder viewHolder = null;
- if (view == null) {
- viewHolder = new ViewHolder();
- view = LayoutInflater.from(mContext).inflate(R.layout.item, null);
- viewHolder.tvTitle = (TextView) view.findViewById(R.id.title);
- viewHolder.btnDel = (Button) view.findViewById(R.id.del);
- view.setTag(viewHolder);
- } else {
- viewHolder = (ViewHolder) view.getTag();
- }
- //为每一个view项设置触控监听
- view.setOnTouchListener(new OnTouchListener() {
- public boolean onTouch(View v, MotionEvent event) {
- final ViewHolder holder = (ViewHolder) v.getTag();
- //当按下时处理
- if (event.getAction() == MotionEvent.ACTION_DOWN) {
- //设置背景为选中状态
- v.setBackgroundResource(R.drawable.mm_listitem_pressed);
- //获取按下时的x轴坐标
- x = event.getX();
- //判断之前是否出现了删除按钮如果存在就隐藏
- if (curDel_btn != null) {
- curDel_btn.setVisibility(View.GONE);
- }
- } else if (event.getAction() == MotionEvent.ACTION_UP) {// 松开处理
- //设置背景为未选中正常状态
- v.setBackgroundResource(R.drawable.mm_listitem_simple);
- //获取松开时的x坐标
- ux = event.getX();
- //判断当前项中按钮控件不为空时
- if (holder.btnDel != null) {
- //按下和松开绝对值差当大于20时显示删除按钮,否则不显示
- if (Math.abs(x - ux) > 20) {
- holder.btnDel.setVisibility(View.VISIBLE);
- curDel_btn = holder.btnDel;
- }
- }
- } else if (event.getAction() == MotionEvent.ACTION_MOVE) {//当滑动时背景为选中状态
- v.setBackgroundResource(R.drawable.mm_listitem_pressed);
- } else {//其他模式
- //设置背景为未选中正常状态
- v.setBackgroundResource(R.drawable.mm_listitem_simple);
- }
- return true;
- }
- });
- viewHolder.tvTitle.setText(this.arrays.get(position));
- //为删除按钮添加监听事件,实现点击删除按钮时删除该项
- viewHolder.btnDel.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- if(curDel_btn!=null)
- curDel_btn.setVisibility(View.GONE);
- arrays.remove(position);
- notifyDataSetChanged();
- }
- });
- return view;
- }
- final static class ViewHolder {
- TextView tvTitle;
- Button btnDel;
- }
- }
第四步:编写MainActivity这里比较简单,不多说直接贴代码
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ListView;
- public class MainActivity extends Activity {
- private ListView mListView;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mListView = (ListView) this.findViewById(R.id.list);
- List<String> list = new ArrayList<String>();
- for(int i=0;i<10;i++){
- list.add("选项"+i);
- }
- //实例化自定义内容适配类
- MyAdapter adapter = new MyAdapter(this,list);
- //为listView设置适配
- mListView.setAdapter(adapter);
- }
- }
第五步:运行效果如图: