-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathScrollListviewDelete.java
116 lines (101 loc) · 3.3 KB
/
ScrollListviewDelete.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.jwzhangjie.scrollview;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.AbsListView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;
public class ScrollListviewDelete extends ListView implements OnScrollListener{
private float minDis = 10;
private float mLastMotionX;// 记住上次X触摸屏的位置
private float mLastMotionY;// 记住上次Y触摸屏的位置
private boolean isLock = false;
public interface ItemClickListener{
void onItemClick(int position);
}
private ItemClickListener onItemClickListener;
public void setOnItemClickListener(ItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
public ScrollListviewDelete(Context context, AttributeSet attrs) {
super(context, attrs);
setOnScrollListener(this);
}
/**
* 如果一个ViewGroup的onInterceptTouchEvent()方法返回true,说明Touch事件被截获,
* 子View不再接收到Touch事件,而是转向本ViewGroup的
* onTouchEvent()方法处理。从Down开始,之后的Move,Up都会直接在onTouchEvent()方法中处理。
* 先前还在处理touch event的child view将会接收到一个 ACTION_CANCEL。
* 如果onInterceptTouchEvent()返回false,则事件会交给child view处理。
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (!isIntercept(ev)) {
DeleteAdapter.ItemDeleteReset();
return false;
}
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
boolean dte = super.dispatchTouchEvent(event);
if (MotionEvent.ACTION_UP == event.getAction() && !dte) {//onItemClick
int position = pointToPosition((int)event.getX(), (int)event.getY());
if (onItemClickListener != null) {
onItemClickListener.onItemClick(position);
}
}
return dte;
}
/**
* 检测是ListView滑动还是item滑动 isLock 一旦判读是item滑动,则在up之前都是返回false
*/
private boolean isIntercept(MotionEvent ev) {
float x = ev.getX();
float y = ev.getY();
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.e("test", "isIntercept ACTION_DOWN "+isLock);
mLastMotionX = x;
mLastMotionY = y;
break;
case MotionEvent.ACTION_MOVE:
Log.e("test", "isIntercept ACTION_MOVE "+isLock);
if (!isLock) {
float deltaX = Math.abs(mLastMotionX - x);
float deltay = Math.abs(mLastMotionY - y);
mLastMotionX = x;
mLastMotionY = y;
if (deltaX > deltay && deltaX > minDis) {
isLock = true;
return false;
}
} else {
return false;
}
break;
case MotionEvent.ACTION_UP:
Log.e("test", "isIntercept ACTION_UP "+isLock);
isLock = false;
break;
case MotionEvent.ACTION_CANCEL:
Log.e("test", "isIntercept ACTION_CANCEL "+isLock);
isLock = false;
break;
}
return true;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState != OnScrollListener.SCROLL_STATE_IDLE) {//认为是滚动,重置
DeleteAdapter.ItemDeleteReset();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
}