Android ListView实现仿iPhone实现左滑删除按钮的简单实例
作者:jingxian 时间:2023-06-15 04:25:59
需要自定义ListView。这里就交FloatDelListView吧。
复写onTouchEvent方法。如下:
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:<BR>// 获取按下的条目视图(child view)
int childCount = getChildCount();
int[] listViewCoords = new int[2];
getLocationOnScreen(listViewCoords);
int x = (int) ev.getRawX() - listViewCoords[0];
int y = (int) ev.getRawY() - listViewCoords[1];
for (int i = 0; i < childCount; i++) {
downChild = getChildAt(i); //
Rect rect = new Rect();
assert downChild != null;
downChild.getHitRect(rect);
int childPosition = getPositionForView(downChild);
if (rect.contains(x, y)) {
downX = ev.getRawX();
int downPosition = childPosition;
velocityTracker = VelocityTracker.obtain();
assert velocityTracker != null;
velocityTracker.addMovement(ev);
break;
}
}
isSwipe = false;
break;
case MotionEvent.ACTION_MOVE:
velocityTracker.addMovement(ev);<BR> // 计算水平和垂直方向移动速度
velocityTracker.computeCurrentVelocity(1000);
float velocityX = Math.abs(velocityTracker.getXVelocity());
float velocityY = Math.abs(velocityTracker.getYVelocity());
<BR> // 水平移动距离
float deltaX = ev.getRawX() - downX;
float deltaMode = Math.abs(deltaX);
if (deltaX > 150) {// right swipe(右滑)
isSwipeToLeft = false;
} else if (deltaX < -150) {// left swipe(左滑)
isSwipeToLeft = true;
}<BR> // 如果水平滑动距离大于零,并且水平滑动速率比垂直大,说明是水平滑动
if (deltaMode > 0 && velocityY < velocityX) {<BR>// 这里的FloatDelButtonLayout是自定义的LinearLayout。
((FloatDelButtonLayout) downChild).showDelButton(ev, isSwipeToLeft);
isSwipe = true;
}
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
downChild.setSelected(false);
if (isSwipe) {
isSwipe = false;
return true;
}
break;
}
return super.onTouchEvent(ev);
}
FloatDelButtonLayou.java :
public class FloatDelButtonLayout extends LinearLayout {
<BR> // 提供删除按钮的接口
private OnDelListener delListener;
<BR> // 当前视图在列表中的索引,在delListener中使用
private int index;
<BR> // 右滑 还是 左滑?<BR> private boolean isSwipeToLeft;<BR>
public void setOnDelListener(OnDelListener listener, int i) {
delListener = listener;
index = i;
}
public FloatDelButtonLayout(Context context) {
super(context, null);
}
public FloatDelButtonLayout(Context context, AttributeSet attrs) {
super(context, attrs, 0);
}
public FloatDelButtonLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
<BR> // 用来显示或者隐藏删除按钮。
public void showDelButton(MotionEvent ev, boolean isSwipeToLeft) {
this.isSwipeToLeft = isSwipeToLeft;
onTouchEvent(ev);
}
private OnClickListener clickDel = new OnClickListener() {
@Override
public void onClick(View v) {
delListener.onDel(index);
}
};
<BR> /**<BR> * 这里的event是我们显示的从FloatDelListView的onTouchEvent里面传进来的,<BR> */
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (MotionEventCompat.getActionMasked(event)) {
case MotionEvent.ACTION_MOVE:<BR> // 获取删除按钮对象,视图layout中必须要有id为del_button的Button标签
Button view = (Button) findViewById(R.id.del_button);
view.setText(R.string.del);<BR> // 设置Button的MarginLayoutParams,当然可以做成各种动作,比如渐隐之类的显示出来。
MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();
assert layoutParams != null;
if (isSwipeToLeft) {
view.setVisibility(View.VISIBLE);
view.setOnClickListener(clickDel);
layoutParams.leftMargin = -200;
} else {
view.setVisibility(View.GONE);
layoutParams.leftMargin = 0;
}
view.setLayoutParams(layoutParams);
invalidate();
break;
}
return super.onTouchEvent(event);
}
public interface OnDelListener {
void onDel(int i);
}
}
标签:listview,左滑,删除,按钮
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java统计字符串中重复字符出现次数的方法
2022-02-15 00:52:16
在C#中使用MSMQ的方法
2023-04-05 20:28:29
Java泛型定义与用法实例详解
2023-11-25 11:50:28
SpringBoot Pom文件依赖及Starter启动器详细介绍
2022-10-08 19:30:20
![](https://img.aspxhome.com/file/2023/7/80987_0s.png)
Android开发中Button组件的使用
2021-07-08 07:18:13
![](https://img.aspxhome.com/file/2023/0/137710_0s.png)
Android蓝牙服务启动流程分析探索
2023-12-20 17:45:35
![](https://img.aspxhome.com/file/2023/1/123281_0s.png)
基于Matlab实现中国象棋的示例代码
2022-09-06 23:47:54
![](https://img.aspxhome.com/file/2023/7/127507_0s.jpg)
Android实现登录界面的注册功能
2021-10-22 01:17:55
![](https://img.aspxhome.com/file/2023/3/106873_0s.jpg)
Android中使用Service实现后台发送邮件功能实例
2022-01-26 16:56:05
Android自定义textview实现竖直滚动跑马灯效果
2023-09-30 07:42:05
C#获取USB事件API实例分析
2021-09-01 13:55:07
SpringBoot整合Elasticsearch7.2.0的实现方法
2023-11-09 19:22:56
![](https://img.aspxhome.com/file/2023/6/113406_0s.png)
浅谈java中String与StringBuffer的不同
2021-11-24 11:54:12
Java二维数组计算集合总结
2023-02-15 16:38:14
![](https://img.aspxhome.com/file/2023/8/63998_0s.jpg)
解决 INSTALL FAILED CONFLICTING PROVIDER的问题方法
2023-01-31 11:42:49
![](https://img.aspxhome.com/file/2023/6/96016_0s.png)
C++中用指向数组的指针作函数参数
2022-08-27 23:11:33
Android studio 快速删除无用资源的方法
2022-10-06 20:53:42
![](https://img.aspxhome.com/file/2023/7/138787_0s.png)
Struts2学习笔记(5)-参数传递方法
2023-08-30 13:03:21
Java内存模型可见性问题相关解析
2023-01-15 06:22:06
Android webview打开本地图片上传实现代码
2023-06-02 14:11:50