以下是代码示例:
public class SwipeRelativeLayout extends RelativeLayout {
private String TAG = "SwipedListView";
private MotionEvent previous = null;
private MotionEvent start = null;
public SwipeRelativeLayout(Context context) {
super(context);
}
public SwipeRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
int action = ev.getAction();
String eventType = "";
switch (action)
{
case MotionEvent.ACTION_CANCEL:
{
eventType = "ACTION_CANCEL";
} break;
case MotionEvent.ACTION_DOWN:
{
eventType = "ACTION_DOWN";
} break;
case MotionEvent.ACTION_UP:
{
eventType = "ACTION_UP";
} break;
case MotionEvent.ACTION_MOVE:
{
eventType = "ACTION_MOVE";
} break;
}
Log.e(TAG, "onInterceptTouchEvent() Type = " + eventType + " X = " + Float.toString(ev.getRawX()) + " Y = " + Float.toString(ev.getRawY()));
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction() ;
String eventType = "";
switch (action)
{
case MotionEvent.ACTION_CANCEL:
{
eventType = "ACTION_CANCEL";
} break;
case MotionEvent.ACTION_DOWN:
{
eventType = "ACTION_DOWN";
} break;
case MotionEvent.ACTION_UP:
{
eventType = "ACTION_UP";
} break;
case MotionEvent.ACTION_MOVE:
{
eventType = "ACTION_MOVE";
} break;
}
Log.e(TAG, "onTouchEvent() Type = " + eventType + " X = " + Float.toString(event.getRawX()) + " Y = " + Float.toString(event.getRawY()));
return false;
}
}
有人可以解释一下,为什么我可以在onInterceptTouchEvent()中看到ontion ACTION_DOWN事件? 正如在developers.android.com的第3点所述。
只要你从这个函数返回false,每个后续事件(包括最后一个)将首先在这里传递给目标的onTouchEvent()。
我做了这个,但stil无法在onInterceptTouchEvent()中看到事件,除了ACTION_DOWN
答案 0 :(得分:0)
只要你从这个函数返回 false ,每个都跟着 活动(包括最后一次)将在这里首先发布 然后到目标的onTouchEvent()。
您将从true
返回onInterceptTouchEvent
,然后您从false
返回onTouchEvent
。您获得ACTION_DOWN
,拦截它,然后您将停止接收流的其余部分的触摸事件。
下次请阅读文档,阅读您的代码,不要在帖子的标题中添加粗略的字词。