MainActivity如下:
package cc.testviewstudy4; import java.util.ArrayList; import java.util.HashMap; import cc.testviewstudy4.ListViewSubClass.OnDeleteListener; import android.os.Bundle; import android.widget.SimpleAdapter; import android.app.Activity; /** * Demo描述: * 关于自定义View的学习(四) * * 自定义View的实现方式大概可以分为三种: * 自绘控件、组合控件、以及继承控件 * 在此Demo中实现继承控件: * 自定义ListView.在ListView的Item上滑动时可出现一个 * 删除View.点击它,可删除ListView中的该条数据 * * 学习资料: * http://blog.csdn.net/guolin_blog/article/details/17357967 * Thank you very much * */ public class MainActivity extends Activity { private ListViewSubClass mListView; private SimpleAdapter mSimpleAdapter; private ArrayList<HashMap<String, Object>> mArrayList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mListView=(ListViewSubClass) findViewById(R.id.listView); mArrayList=new ArrayList<HashMap<String,Object>>(); HashMap<String, Object> hashMap=null; for (int i = 0; i <12; i++) { hashMap=new HashMap<String, Object>(); hashMap.put("name", "小明"+i); mArrayList.add(hashMap); } mSimpleAdapter=new SimpleAdapter(this, mArrayList, R.layout.listviewitem, new String []{"name"}, new int[]{R.id.name}); mListView.setAdapter(mSimpleAdapter); mListView.setOnDeleteListener(new OnDeleteListener() { @Override public void onDelete(int index) { mArrayList.remove(index); mSimpleAdapter.notifyDataSetChanged(); } }); } }
ListViewSubClass如下:
package cc.testviewstudy4; import android.content.Context; import android.util.AttributeSet; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.widget.ListView; import android.widget.RelativeLayout; public class ListViewSubClass extends ListView implements OnGestureListener,OnTouchListener { private GestureDetector mGestureDetector; private OnDeleteListener mDeleteListener; private View mDeleteView; private ViewGroup mListViewItemViewGroup; private int selectedItem; private boolean isDeleteShowing; public ListViewSubClass(Context context) { super(context); } public ListViewSubClass(Context context, AttributeSet attrs) { super(context, attrs); // 监听Touch this.setOnTouchListener(this); // 手势 mGestureDetector = new GestureDetector(context, this); } public ListViewSubClass(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void setOnDeleteListener(OnDeleteListener onDeleteListener){ mDeleteListener=onDeleteListener; } //定义一个接口 public interface OnDeleteListener { void onDelete(int index); } //------>以下方法为OnTouchListener接口的实现 @Override public boolean onTouch(View v, MotionEvent event) { //if-->当有删除View的时候.我们触摸该ListView时将其消失 if (isDeleteShowing) { mListViewItemViewGroup.removeView(mDeleteView); mDeleteView = null; isDeleteShowing = false; return false; //else-->其余时候交给GestureDetector处理 } else { return mGestureDetector.onTouchEvent(event); } } //------>以下方法为OnTouchListener接口的实现 //------>以下方法为OnGestureListener接口的实现 @Override public boolean onDown(MotionEvent e) { if (!isDeleteShowing) { selectedItem = pointToPosition((int) e.getX(), (int) e.getY()); } return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) { //当删除View没有显示并且是水平滑动时我们显示删除View if (!isDeleteShowing && Math.abs(velocityX) > Math.abs(velocityY)) { mDeleteView = LayoutInflater.from(getContext()).inflate(R.layout.delete, null); mDeleteView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mListViewItemViewGroup.removeView(mDeleteView); mDeleteView = null; isDeleteShowing = false; mDeleteListener.onDelete(selectedItem); } }); //找到当前的Item mListViewItemViewGroup = (ViewGroup) getChildAt(selectedItem-getFirstVisiblePosition()); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); params.addRule(RelativeLayout.CENTER_VERTICAL); mListViewItemViewGroup.addView(mDeleteView, params); isDeleteShowing = true; } return false; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return false; } //------>以上方法为OnGestureListener接口的实现 }
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" > <cc.testviewstudy4.ListViewSubClass android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout>
delete.xml如下:
<?xml version="1.0" encoding="utf-8"?> <Button xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/deleteButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/deletebutton" > </Button>
listviewitem如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="70dip" > <TextView android:id="@+id/name" android:layout_width="100dip" android:layout_height="wrap_content" android:textSize="25sp" android:layout_centerVertical="true" /> </RelativeLayout>