listview滑动删除item的一个方案

功能:在listview上,向右滑动,直接删除一个item

主要实现思路:

检测listview的touch操作,down时,记录位置,up时,做删除。


部分代码:

mListView.setOnTouchListener(new OnTouchListener() {
			
			private int mDownX = 0;
			private int mDownY = 0;
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					mDownX = Math.round(event.getX());
					mDownY = Math.round(event.getY());
					break;
				case MotionEvent.ACTION_MOVE:
					break;
				case MotionEvent.ACTION_UP:
				case MotionEvent.ACTION_CANCEL: {
					int upX = Math.round(event.getX());
					int upY = Math.round(event.getY());
					// 获取2次点击的item位置  item以外的pos是-1
					int downPosition = ((ListView)v).pointToPosition(mDownX, mDownY);
					int upPosition = ((ListView)v).pointToPosition(upX, upY);
					// 2次都在同一个item内  且有50的滑动距离
					if (downPosition == upPosition && (upX - mDownX) > 20) {
						// 如果没在动画中 且 点击在有效item内 才做处理
						boolean willRemove = (!mIsRemoving) && (upPosition >= 0);
						if (willRemove) {
							// 做删除动作  先动画  然后动画结束后做删除
							
							// 需要先获取第一个可见的view  因为getChildAt获取view的时候,坐标是从第一个可见view开始的
							int firstVisiableIndex = ((ListView)v).getFirstVisiblePosition();
							final int positionToRemove = upPosition;
							View viewToRemove = ((ListView)v).getChildAt(upPosition - firstVisiableIndex);
							// 移除动画  移动+渐隐
							Animation transAni = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
									Animation.RELATIVE_TO_SELF, 1.0f,
									Animation.RELATIVE_TO_SELF, 0,
									Animation.RELATIVE_TO_SELF, 0);
							Animation alphaAni = new AlphaAnimation(1.0f, 0);
							AnimationSet removeAni = new AnimationSet(true);
							removeAni.addAnimation(transAni);
							removeAni.addAnimation(alphaAni);
							removeAni.setDuration(500);
							// 设置回调  在动画结束后做删除数据
							removeAni.setAnimationListener(new AnimationListener() {
								
								@Override
								public void onAnimationStart(Animation animation) {
									mIsRemoving = true;
								}
								
								@Override
								public void onAnimationRepeat(Animation animation) {
									
								}
								
								@Override
								public void onAnimationEnd(Animation animation) {
									// 结束时删除数据
			
									// 删除外面的数据
									mAdapter.removeData(positionToRemove);
									// animation.cancel();	// 调用cancel会导致二次进入onAnimationEnd
									
									mIsRemoving = false;
								}
							});
							// 加入动画
							viewToRemove.startAnimation(removeAni);

						}
					}
				}
					break;
				default:
					break;
				}
				
				return false;
			}
		});

点击item时,item不高亮的处理:重写adapter的2个函数

	// 处理掉item点击高亮  同时也导致item的click和longclick无法响应
	@Override
	public boolean areAllItemsEnabled() {
		return false;
	}
	
	@Override
	public boolean isEnabled(int position) {
		return false;
	}


效果:





  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值