MainActivity
package com.example.administrator.listview; import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.VelocityTracker; import android.view.View; import android.view.ViewConfiguration; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity { float x = 0; float y = 0; PopupWindow popupWindow; private List<String> infoList; public static int statusBarHeight = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); infoList = new ArrayList<String>(); for (int i = 0; i < 20; i++) { infoList.add("item" + i); } MyDeleteListView listview = new MyDeleteListView(this); final MyAdapter adapter = new MyAdapter(this, infoList); listview.setAdapter(adapter); listview.setOnItemDeleteCallback(new MyDeleteListView.OnDeleteCallback() { @Override public void onDelete(AdapterView<?> parent, int deletePosition) { parent.getChildAt(deletePosition); Toast.makeText(MainActivity.this, infoList.get(deletePosition) + ",被删除了", Toast.LENGTH_SHORT).show(); infoList.remove(deletePosition); adapter.notifyDataSetChanged(); } }); listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, infoList.get(position) + ",被点击了", Toast.LENGTH_SHORT) .show(); } }); setContentView(listview); } public class MyAdapter extends BaseAdapter implements ListAdapter { private List<String> infoList; private LayoutInflater inflater; /** * @param mainActivity * @param infoList */ public MyAdapter(Context context, List<String> infoList) { this.infoList = infoList; this.inflater = LayoutInflater.from(context); } @Override public int getCount() { return infoList.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (null == convertView) convertView = inflater.inflate(R.layout.item, null); final TextView textview = (TextView) convertView .findViewById(R.id.item_tv); textview.setText(infoList.get(position)); return convertView; } } }
MyDeleteListView
package com.example.administrator.listview; import android.content.Context; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.VelocityTracker; import android.view.View; import android.view.ViewConfiguration; import android.widget.AdapterView; import android.widget.ListView; import android.widget.PopupWindow; public class MyDeleteListView extends ListView { private LayoutInflater inflater; public MyDeleteListView(Context context) { super(context); inflater = LayoutInflater.from(context); ViewConfiguration vc = ViewConfiguration.get(context); mSlop = vc.getScaledTouchSlop(); } public MyDeleteListView(Context context, AttributeSet attrs) { super(context, attrs); } public MyDeleteListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: handleActionDown(ev); break; case MotionEvent.ACTION_MOVE: return handleActionMove(ev); case MotionEvent.ACTION_UP: handleActionUp(ev); break; } return super.onTouchEvent(ev); } private float mDownX; private float mDownY; /** * 认为是用户滑动的�?��距离 */ private int mSlop; private int mDownPosition; private OnDeleteCallback onDeleteCallback; /** * 用来标记用户是否正在滑动�? */ private boolean mSwiping; /** * 按下的item对应的View */ private View mDownView; /** * 滑动速度�?���? */ private VelocityTracker mVelocityTracker; /** * item的宽�? */ private int mViewWidth; /** * item的高�? */ private int mViewHeight; /** * 按下事件处理 * * @param ev * @return */ private void handleActionDown(MotionEvent ev) { mDownX = ev.getX(); mDownY = ev.getY(); mDownPosition = pointToPosition((int) mDownX, (int) mDownY); if (mDownPosition == AdapterView.INVALID_POSITION) { return; } mDownView = getChildAt(mDownPosition - getFirstVisiblePosition()); if (mDownView != null) { mViewWidth = mDownView.getWidth(); mViewHeight = mDownView.getHeight(); } // 加入速度�?�� mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(ev); } private PopupWindow popupWindow; /** * 手指抬起的事件处�? * * @param ev */ private void handleActionUp(MotionEvent ev) { if (mVelocityTracker == null || mDownView == null || !mSwiping) { return; } if (ev.getX() < mDownX && Math.abs(ev.getX() - mDownX) > Math.abs(ev.getY() - mDownY)) {// x轴左方向滑动 showDeletePopup(); } mSwiping = false; } private void showDeletePopup() { View deleteView = inflater.inflate(R.layout.layout_popupwindow, null); deleteView.findViewById(R.id.popupwindow_btn).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); if (onDeleteCallback != null) { onDeleteCallback.onDelete(MyDeleteListView.this, mDownPosition); } } }); //measure deleteView 后才能获取宽�? int w = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); int h = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); deleteView.measure(w, h); int height = deleteView.getMeasuredHeight(); int width = deleteView.getMeasuredWidth(); popupWindow = new PopupWindow(deleteView, LayoutParams.WRAP_CONTENT, android.view.WindowManager.LayoutParams.WRAP_CONTENT, true); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));// 要设置背�? popupWindow.setAnimationStyle(R.style.AnimationPreview);// 动画 popupWindow.showAsDropDown(mDownView, mViewWidth - width, -(mViewHeight - (mViewHeight - height) / 2)); popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); popupWindow.setTouchable(true); } /** * 处理手指滑动的方�? * * @param ev * @return */ private boolean handleActionMove(MotionEvent ev) { if (mVelocityTracker == null || mDownView == null) { return super.onTouchEvent(ev); } float deltaX = ev.getX() - mDownX; float deltaY = ev.getY() - mDownY; // X方向滑动的距离大于mSlop并且Y方向滑动的距离小于mSlop,表示可以滑�? if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < mSlop) { mSwiping = true; // 当手指滑动item,取消item的点击事件,不然我们滑动Item也伴随着item点击事件的发�? MotionEvent cancelEvent = MotionEvent.obtain(ev); cancelEvent .setAction(MotionEvent.ACTION_CANCEL | (ev.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); onTouchEvent(cancelEvent); } if (mSwiping) { // 手指滑动的时�?返回true,表示SwipeDismissListView自己处理onTouchEvent,其他的就交给父类来处�? return true; } return super.onTouchEvent(ev); } public void setOnItemDeleteCallback(OnDeleteCallback onDeleteCallback) { this.onDeleteCallback = onDeleteCallback; } /** * 删除的回调接�? * * @author xiaanming * */ public interface OnDeleteCallback { public void onDelete(AdapterView<?> parent, int deletePosition); } }
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.administrator.listview.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> </RelativeLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="20dp" > <TextView android:id="@+id/item_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
layout_popupwindow.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" > <Button android:id="@+id/popupwindow_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ff0000" android:padding="10dp" android:text="删 除" android:textColor="#ffffff" /> </LinearLayout>menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_settings" android:orderInCategory="100" android:showAsAction="never" /> </menu>