大家是不是觉得ios列表的滑动删除效果很酷炫?不用羡慕android也可以实现相同的效果

并且可以自定义效果,比如左滑删除,置顶,收藏,分享等等

其实就是自定义listview重写listview方法。废话不多少说,上干货:

这是封装好的listview重写方法

 import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Interpolator;
import android.widget.ListAdapter;
import android.widget.ListView; public class SwipeMenuListView extends ListView { private static final int TOUCH_STATE_NONE = 0;
private static final int TOUCH_STATE_X = 1;
private static final int TOUCH_STATE_Y = 2; public static final int DIRECTION_LEFT = 1;
public static final int DIRECTION_RIGHT = -1;
private int mDirection = 1;//swipe from right to left by default private int MAX_Y = 5;
private int MAX_X = 3;
private float mDownX;
private float mDownY;
private int mTouchState;
private int mTouchPosition;
private SwipeMenuLayout mTouchView;
private OnSwipeListener mOnSwipeListener; private SwipeMenuCreator mMenuCreator;
private OnMenuItemClickListener mOnMenuItemClickListener;
private OnMenuStateChangeListener mOnMenuStateChangeListener;
private Interpolator mCloseInterpolator;
private Interpolator mOpenInterpolator; public SwipeMenuListView(Context context) {
super(context);
init();
} public SwipeMenuListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} public SwipeMenuListView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} private void init() {
MAX_X = dp2px(MAX_X);
MAX_Y = dp2px(MAX_Y);
mTouchState = TOUCH_STATE_NONE;
} @Override
public void setAdapter(ListAdapter adapter) {
super.setAdapter(new SwipeMenuAdapter(getContext(), adapter) {
@Override
public void createMenu(SwipeMenu menu) {
if (mMenuCreator != null) {
mMenuCreator.create(menu);
}
} @Override
public void onItemClick(SwipeMenuView view, SwipeMenu menu,
int index) {
boolean flag = false;
if (mOnMenuItemClickListener != null) {
flag = mOnMenuItemClickListener.onMenuItemClick(
view.getPosition(), menu, index);
}
if (mTouchView != null && !flag) {
mTouchView.smoothCloseMenu();
}
}
});
} public void setCloseInterpolator(Interpolator interpolator) {
mCloseInterpolator = interpolator;
} public void setOpenInterpolator(Interpolator interpolator) {
mOpenInterpolator = interpolator;
} public Interpolator getOpenInterpolator() {
return mOpenInterpolator;
} public Interpolator getCloseInterpolator() {
return mCloseInterpolator;
} @Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return super.onInterceptTouchEvent(ev);
} @Override
public boolean onTouchEvent(MotionEvent ev) {
if (ev.getAction() != MotionEvent.ACTION_DOWN && mTouchView == null)
return super.onTouchEvent(ev);
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
int oldPos = mTouchPosition;
mDownX = ev.getX();
mDownY = ev.getY();
mTouchState = TOUCH_STATE_NONE; mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY()); if (mTouchPosition == oldPos && mTouchView != null
&& mTouchView.isOpen()) {
mTouchState = TOUCH_STATE_X;
mTouchView.onSwipe(ev);
return true;
} View view = getChildAt(mTouchPosition - getFirstVisiblePosition()); if (mTouchView != null && mTouchView.isOpen()) {
mTouchView.smoothCloseMenu();
mTouchView = null;
// return super.onTouchEvent(ev);
// try to cancel the touch event
MotionEvent cancelEvent = MotionEvent.obtain(ev);
cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
onTouchEvent(cancelEvent);
if (mOnMenuStateChangeListener != null) {
mOnMenuStateChangeListener.onMenuClose(oldPos);
}
return true;
}
if (view instanceof SwipeMenuLayout) {
mTouchView = (SwipeMenuLayout) view;
mTouchView.setSwipeDirection(mDirection);
}
if (mTouchView != null) {
mTouchView.onSwipe(ev);
}
break;
case MotionEvent.ACTION_MOVE:
float dy = Math.abs((ev.getY() - mDownY));
float dx = Math.abs((ev.getX() - mDownX));
if (mTouchState == TOUCH_STATE_X) {
if (mTouchView != null) {
mTouchView.onSwipe(ev);
}
getSelector().setState(new int[]{0});
ev.setAction(MotionEvent.ACTION_CANCEL);
super.onTouchEvent(ev);
return true;
} else if (mTouchState == TOUCH_STATE_NONE) {
if (Math.abs(dy) > MAX_Y) {
mTouchState = TOUCH_STATE_Y;
} else if (dx > MAX_X) {
mTouchState = TOUCH_STATE_X;
if (mOnSwipeListener != null) {
mOnSwipeListener.onSwipeStart(mTouchPosition);
}
}
}
break;
case MotionEvent.ACTION_UP:
if (mTouchState == TOUCH_STATE_X) {
if (mTouchView != null) {
boolean isBeforeOpen = mTouchView.isOpen();
mTouchView.onSwipe(ev);
boolean isAfterOpen = mTouchView.isOpen();
if (isBeforeOpen != isAfterOpen && mOnMenuStateChangeListener != null) {
if (isAfterOpen) {
mOnMenuStateChangeListener.onMenuOpen(mTouchPosition);
} else {
mOnMenuStateChangeListener.onMenuClose(mTouchPosition);
}
}
if (!isAfterOpen) {
mTouchPosition = -1;
mTouchView = null;
}
}
if (mOnSwipeListener != null) {
mOnSwipeListener.onSwipeEnd(mTouchPosition);
}
ev.setAction(MotionEvent.ACTION_CANCEL);
super.onTouchEvent(ev);
return true;
}
break;
}
return super.onTouchEvent(ev);
} public void smoothOpenMenu(int position) {
if (position >= getFirstVisiblePosition()
&& position <= getLastVisiblePosition()) {
View view = getChildAt(position - getFirstVisiblePosition());
if (view instanceof SwipeMenuLayout) {
mTouchPosition = position;
if (mTouchView != null && mTouchView.isOpen()) {
mTouchView.smoothCloseMenu();
}
mTouchView = (SwipeMenuLayout) view;
mTouchView.setSwipeDirection(mDirection);
mTouchView.smoothOpenMenu();
}
}
} public void smoothCloseMenu(){
if (mTouchView != null && mTouchView.isOpen()) {
mTouchView.smoothCloseMenu();
}
} private int dp2px(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
getContext().getResources().getDisplayMetrics());
} public void setMenuCreator(SwipeMenuCreator menuCreator) {
this.mMenuCreator = menuCreator;
} public void setOnMenuItemClickListener(
OnMenuItemClickListener onMenuItemClickListener) {
this.mOnMenuItemClickListener = onMenuItemClickListener;
} public void setOnSwipeListener(OnSwipeListener onSwipeListener) {
this.mOnSwipeListener = onSwipeListener;
} public void setOnMenuStateChangeListener(OnMenuStateChangeListener onMenuStateChangeLi stener) {
mOnMenuStateChangeListener = onMenuStateChangeListener;
} public static interface OnMenuItemClickListener {
boolean onMenuItemClick(int position, SwipeMenu menu, int index);
} public static interface OnSwipeListener {
void onSwipeStart(int position); void onSwipeEnd(int position);
} public static interface OnMenuStateChangeListener {
void onMenuOpen(int position); void onMenuClose(int position);
} public void setSwipeDirection(int direction) {
mDirection = direction;
}
}

上面是自定义listview,下面叫大家怎么使用

在xml文件中需要使用listview方法时,不要直接用<listview/>改用<xxx.xx.xx.swipemenulistview.SwipeMenuListView

android:id="@+id/lv_my_case"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>前面xx是自己的包名例如cn.sy.gdxz

这只是准备工作,真正的业务操作在下面

private void initSlide() {
// TODO Auto-generated method stub
// step 1. 创建实例
SwipeMenuCreator creator = new SwipeMenuCreator() {
@SuppressLint("ResourceAsColor")
@Override public void create(SwipeMenu menu) {
LogUtils.v("SwipeMenuCreator==create");
SwipeMenuItem deleteItem = new SwipeMenuItem(
getApplicationContext());
deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
0x3F, 0x25)));
// set item width
deleteItem.setWidth(dp2px(90));
// set a icon图标
deleteItem.setIcon(R.drawable.ic_delete);
//set a 标题
deleteItem.setTitle("删除");
deleteItem.setTitleSize(18);
deleteItem.setTitleColor(getResources().getColor(R.color.white));
// add to menu
menu.addMenuItem(deleteItem);
}
};
// set creator
lv_my_case.setMenuCreator(creator); // step 2. listener item 点击事件
lv_my_case.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() { @Override
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
LogUtils.v("onMenuItemClick==position="+position+"index=="+index);
switch (index) {
case 0://
// open处理滑动事件 //处理滑动处理的第一个事件,比如删除break;
case 1:
//这里处理滑动出来的第二项比如收藏,置顶等等 break;
}
return false;
} });
// set SwipeListener lv_my_case.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() { @Override
public void onSwipeStart(int position) {
// swipe start
LogUtils.v("onSwipeEnd+"+position);
} @Override
public void onSwipeEnd(int position) {
// swipe end
LogUtils.v("onSwipeEnd"+position);
}
}); // set MenuStateChangeListener
lv_my_case.setOnMenuStateChangeListener(new SwipeMenuListView.OnMenuStateChangeListener() {
@Override
public void onMenuOpen(int position) {
LogUtils.v("onMenuOpen"+position);
} @Override
public void onMenuClose(int position) {
LogUtils.v("onMenuClose"+position);
}
}); }

private int dp2px(int dp) {
          return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
          getResources().getDisplayMetrics());
   }

 

到这里滑动删除就分享完了。只是记录一下学习状态,不足之处大家互相交流,互相学习