android中列表的滑动删除仿ios滑动删除
jopen
9年前
大家是不是觉得ios列表的滑动删除效果很酷炫?不用羡慕android也可以实现相同的效果
并且可以自定义效果,比如左滑删除,置顶,收藏,分享等等
其实就是自定义listview重写listview方法。废话不多少说,上干货:
这是封装好的listview重写方法
1 import android.content.Context; 2 import android.util.AttributeSet; 3 import android.util.TypedValue; 4 import android.view.MotionEvent; 5 import android.view.View; 6 import android.view.animation.Interpolator; 7 import android.widget.ListAdapter; 8 import android.widget.ListView; 9 10 14 public class SwipeMenuListView extends ListView { 15 16 private static final int TOUCH_STATE_NONE = 0; 17 private static final int TOUCH_STATE_X = 1; 18 private static final int TOUCH_STATE_Y = 2; 19 20 public static final int DIRECTION_LEFT = 1; 21 public static final int DIRECTION_RIGHT = -1; 22 private int mDirection = 1;//swipe from right to left by default 23 24 private int MAX_Y = 5; 25 private int MAX_X = 3; 26 private float mDownX; 27 private float mDownY; 28 private int mTouchState; 29 private int mTouchPosition; 30 private SwipeMenuLayout mTouchView; 31 private OnSwipeListener mOnSwipeListener; 32 33 private SwipeMenuCreator mMenuCreator; 34 private OnMenuItemClickListener mOnMenuItemClickListener; 35 private OnMenuStateChangeListener mOnMenuStateChangeListener; 36 private Interpolator mCloseInterpolator; 37 private Interpolator mOpenInterpolator; 38 39 public SwipeMenuListView(Context context) { 40 super(context); 41 init(); 42 } 43 44 public SwipeMenuListView(Context context, AttributeSet attrs, int defStyle) { 45 super(context, attrs, defStyle); 46 init(); 47 } 48 49 public SwipeMenuListView(Context context, AttributeSet attrs) { 50 super(context, attrs); 51 init(); 52 } 53 54 private void init() { 55 MAX_X = dp2px(MAX_X); 56 MAX_Y = dp2px(MAX_Y); 57 mTouchState = TOUCH_STATE_NONE; 58 } 59 60 @Override 61 public void setAdapter(ListAdapter adapter) { 62 super.setAdapter(new SwipeMenuAdapter(getContext(), adapter) { 63 @Override 64 public void createMenu(SwipeMenu menu) { 65 if (mMenuCreator != null) { 66 mMenuCreator.create(menu); 67 } 68 } 69 70 @Override 71 public void onItemClick(SwipeMenuView view, SwipeMenu menu, 72 int index) { 73 boolean flag = false; 74 if (mOnMenuItemClickListener != null) { 75 flag = mOnMenuItemClickListener.onMenuItemClick( 76 view.getPosition(), menu, index); 77 } 78 if (mTouchView != null && !flag) { 79 mTouchView.smoothCloseMenu(); 80 } 81 } 82 }); 83 } 84 85 public void setCloseInterpolator(Interpolator interpolator) { 86 mCloseInterpolator = interpolator; 87 } 88 89 public void setOpenInterpolator(Interpolator interpolator) { 90 mOpenInterpolator = interpolator; 91 } 92 93 public Interpolator getOpenInterpolator() { 94 return mOpenInterpolator; 95 } 96 97 public Interpolator getCloseInterpolator() { 98 return mCloseInterpolator; 99 } 100 101 @Override 102 public boolean onInterceptTouchEvent(MotionEvent ev) { 103 return super.onInterceptTouchEvent(ev); 104 } 105 106 @Override 107 public boolean onTouchEvent(MotionEvent ev) { 108 if (ev.getAction() != MotionEvent.ACTION_DOWN && mTouchView == null) 109 return super.onTouchEvent(ev); 110 int action = ev.getAction(); 111 switch (action) { 112 case MotionEvent.ACTION_DOWN: 113 int oldPos = mTouchPosition; 114 mDownX = ev.getX(); 115 mDownY = ev.getY(); 116 mTouchState = TOUCH_STATE_NONE; 117 118 mTouchPosition = pointToPosition((int) ev.getX(), (int) ev.getY()); 119 120 if (mTouchPosition == oldPos && mTouchView != null 121 && mTouchView.isOpen()) { 122 mTouchState = TOUCH_STATE_X; 123 mTouchView.onSwipe(ev); 124 return true; 125 } 126 127 View view = getChildAt(mTouchPosition - getFirstVisiblePosition()); 128 129 if (mTouchView != null && mTouchView.isOpen()) { 130 mTouchView.smoothCloseMenu(); 131 mTouchView = null; 132 // return super.onTouchEvent(ev); 133 // try to cancel the touch event 134 MotionEvent cancelEvent = MotionEvent.obtain(ev); 135 cancelEvent.setAction(MotionEvent.ACTION_CANCEL); 136 onTouchEvent(cancelEvent); 137 if (mOnMenuStateChangeListener != null) { 138 mOnMenuStateChangeListener.onMenuClose(oldPos); 139 } 140 return true; 141 } 142 if (view instanceof SwipeMenuLayout) { 143 mTouchView = (SwipeMenuLayout) view; 144 mTouchView.setSwipeDirection(mDirection); 145 } 146 if (mTouchView != null) { 147 mTouchView.onSwipe(ev); 148 } 149 break; 150 case MotionEvent.ACTION_MOVE: 151 float dy = Math.abs((ev.getY() - mDownY)); 152 float dx = Math.abs((ev.getX() - mDownX)); 153 if (mTouchState == TOUCH_STATE_X) { 154 if (mTouchView != null) { 155 mTouchView.onSwipe(ev); 156 } 157 getSelector().setState(new int[]{0}); 158 ev.setAction(MotionEvent.ACTION_CANCEL); 159 super.onTouchEvent(ev); 160 return true; 161 } else if (mTouchState == TOUCH_STATE_NONE) { 162 if (Math.abs(dy) > MAX_Y) { 163 mTouchState = TOUCH_STATE_Y; 164 } else if (dx > MAX_X) { 165 mTouchState = TOUCH_STATE_X; 166 if (mOnSwipeListener != null) { 167 mOnSwipeListener.onSwipeStart(mTouchPosition); 168 } 169 } 170 } 171 break; 172 case MotionEvent.ACTION_UP: 173 if (mTouchState == TOUCH_STATE_X) { 174 if (mTouchView != null) { 175 boolean isBeforeOpen = mTouchView.isOpen(); 176 mTouchView.onSwipe(ev); 177 boolean isAfterOpen = mTouchView.isOpen(); 178 if (isBeforeOpen != isAfterOpen && mOnMenuStateChangeListener != null) { 179 if (isAfterOpen) { 180 mOnMenuStateChangeListener.onMenuOpen(mTouchPosition); 181 } else { 182 mOnMenuStateChangeListener.onMenuClose(mTouchPosition); 183 } 184 } 185 if (!isAfterOpen) { 186 mTouchPosition = -1; 187 mTouchView = null; 188 } 189 } 190 if (mOnSwipeListener != null) { 191 mOnSwipeListener.onSwipeEnd(mTouchPosition); 192 } 193 ev.setAction(MotionEvent.ACTION_CANCEL); 194 super.onTouchEvent(ev); 195 return true; 196 } 197 break; 198 } 199 return super.onTouchEvent(ev); 200 } 201 202 public void smoothOpenMenu(int position) { 203 if (position >= getFirstVisiblePosition() 204 && position <= getLastVisiblePosition()) { 205 View view = getChildAt(position - getFirstVisiblePosition()); 206 if (view instanceof SwipeMenuLayout) { 207 mTouchPosition = position; 208 if (mTouchView != null && mTouchView.isOpen()) { 209 mTouchView.smoothCloseMenu(); 210 } 211 mTouchView = (SwipeMenuLayout) view; 212 mTouchView.setSwipeDirection(mDirection); 213 mTouchView.smoothOpenMenu(); 214 } 215 } 216 } 217 218 public void smoothCloseMenu(){ 219 if (mTouchView != null && mTouchView.isOpen()) { 220 mTouchView.smoothCloseMenu(); 221 } 222 } 223 224 private int dp2px(int dp) { 225 return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, 226 getContext().getResources().getDisplayMetrics()); 227 } 228 229 public void setMenuCreator(SwipeMenuCreator menuCreator) { 230 this.mMenuCreator = menuCreator; 231 } 232 233 public void setOnMenuItemClickListener( 234 OnMenuItemClickListener onMenuItemClickListener) { 235 this.mOnMenuItemClickListener = onMenuItemClickListener; 236 } 237 238 public void setOnSwipeListener(OnSwipeListener onSwipeListener) { 239 this.mOnSwipeListener = onSwipeListener; 240 } 241 242 public void setOnMenuStateChangeListener(OnMenuStateChangeListener onMenuStateChangeLi stener) { 243 mOnMenuStateChangeListener = onMenuStateChangeListener; 244 } 245 246 public static interface OnMenuItemClickListener { 247 boolean onMenuItemClick(int position, SwipeMenu menu, int index); 248 } 249 250 public static interface OnSwipeListener { 251 void onSwipeStart(int position); 252 253 void onSwipeEnd(int position); 254 } 255 256 public static interface OnMenuStateChangeListener { 257 void onMenuOpen(int position); 258 259 void onMenuClose(int position); 260 } 261 262 public void setSwipeDirection(int direction) { 263 mDirection = direction; 264 } 265 }
上面是自定义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()); }
到这里滑动删除就分享完了。只是记录一下学习状态,不足之处大家互相交流,互相学习