Touch事件和ScrollView的冲突

来源:互联网 发布:淘宝卖家的钱在哪里 编辑:程序博客网 时间:2024/06/07 05:52
Touch事件和ScrollView的冲突0作者:沐雨Silence发布于 8小时前访问(21)评论(0)android中的事件类型分为按键事件和屏幕触摸事件,Touch事件是屏幕触摸事件的基础事件,有必要对它进行深入的了解。一个最简单的屏幕触摸动作触发了一系列Touch事件:ACTION_DOWN->ACTION_MOVE->ACTION_MOVE->ACTION_MOVE...->ACTION_MOVE->ACTION_UP当屏幕中包含一个ViewGroup,而这个ViewGroup又包含一个子view,这个时候android系统如何处理Touch事件呢?到底是ViewGroup来处理Touch事件,还是子view来处理Touch事件呢?我只能很肯定的对你说不一定。呵呵,为什么呢?看看下面我的调查结果你就明白了。android系统中的每个View的子类都具有下面三个和TouchEvent处理密切相关的方法:1)public boolean dispatchTouchEvent(MotionEvent ev) 这个方法用来分发TouchEvent2)public boolean onInterceptTouchEvent(MotionEvent ev) 这个方法用来拦截TouchEvent3)public boolean onTouchEvent(MotionEvent ev) 这个方法用来处理TouchEvent当TouchEvent发生时,首先Activity将TouchEvent传递给最顶层的View, TouchEvent最先到达最顶层 view 的 dispatchTouchEvent ,然后由 dispatchTouchEvent 方法进行分发,如果dispatchTouchEvent返回true ,则交给这个view的onTouchEvent处理,如果dispatchTouchEvent返回 false ,则交给这个 view 的 interceptTouchEvent 方法来决定是否要拦截这个事件,如果 interceptTouchEvent 返回 true ,也就是拦截掉了,则交给它的 onTouchEvent 来处理,如果 interceptTouchEvent 返回 false ,那么就传递给子 view ,由子 view 的 dispatchTouchEvent 再来开始这个事件的分发。如果事件传递到某一层的子 view 的 onTouchEvent 上了,这个方法返回了 false ,那么这个事件会从这个 view 往上传递,都是 onTouchEvent 来接收。而如果传递到最上面的 onTouchEvent 也返回 false 的话,这个事件就会“消失”,而且接收不到下一次事件。以上纯文字描述可能比较难理解 一下是代码演示 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 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154package com.example.androidmenutoabhost;import java.util.ArrayList;import java.util.logging.Logger;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.Button;import android.widget.ImageView;import android.widget.LinearLayout;public class ActivityTouchTest extends Activity { Button btn_touch_action; LinearLayout add_view; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_tab_group_test); initLayout(); } private void initLayout(){ btn_touch_action = (Button) this.findViewById(R.id.btn_touch_action); add_view = (LinearLayout) this.findViewById(R.id.add_view); OnTouchMoveViewListener moveViewListener; moveViewListener = new OnTouchMoveViewListener(add_view) { @Override public void onMoving(View v, MotionEvent event, int coordX, int coordY) { // TODO Auto-generated method stub System.out.println("===>onMoving"); } @Override public void onMoveStart(View v, MotionEvent event) { // TODO Auto-generated method stub System.out.println("===>onMoveStart"); add_view.requestDisallowInterceptTouchEvent(true); } @Override public void onMoveEnd(View v, MotionEvent event) { // TODO Auto-generated method stub System.out.println("===>onMoveEnd"); } }; btn_touch_action.setOnTouchListener(moveViewListener); } public abstract class OnTouchMoveViewListener implements OnTouchListener { private int lastX; private int lastY; private View rl_vessel;//容器 public OnTouchMoveViewListener(View rl_vessel) { super(); this.rl_vessel = rl_vessel; } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); onMoveStart(v, event); System.out.println("===>ACTION_DOWN"); break; case MotionEvent.ACTION_MOVE: System.out.println("===>ACTION_MOVE"); int dx =(int)event.getRawX() - lastX; int dy =(int)event.getRawY() - lastY; int left = v.getLeft() + dx; int top = v.getTop() + dy; int right = v.getRight() + dx; int bottom = v.getBottom() + dy; if(left < 0){ left = 0; right = left + v.getWidth(); } if(right > rl_vessel.getWidth()){ right = rl_vessel.getWidth(); left = right - v.getWidth(); } if(top < 0){ top = 0; bottom = top + v.getHeight(); } if(bottom > rl_vessel.getHeight()){ bottom = rl_vessel.getHeight(); top = bottom - v.getHeight(); } v.layout(left, top, right, bottom); Logger.getLogger("OnTouchMoveViewListener").info("position:" + left +", " + top + ", " + right + ", " + bottom); lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); onMoving(v, event, left, top); break; case MotionEvent.ACTION_UP: System.out.println("===>ACTION_UP"); onMoveEnd(v, event); break; default: break; } return true; } /** * 开始移动 *

Title: onMoveStart

*

Description:

* @param v * @param event */ public abstract void onMoveStart(View v, MotionEvent event); /** * 移动中 *

Title: onMoving

*

Description:

* @param v * @param event * @param coordX 移动控件的当前x坐标 * @param coordY 移动控件的当前y坐标 */ public abstract void onMoving(View v, MotionEvent event, int coordX, int coordY); /** * 移动结束 *

Title: onMoveEnd

*

Description:

* @param v * @param event */ public abstract void onMoveEnd(View v, MotionEvent event); }}布局文件 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 99100101102103104105106107108109110111112113114115116117 以上代码不难发现 在onMoveStart的时候对 ScrollView中的LinearLayout进行设置add_view.requestDisallowInterceptTouchEvent(true);即可声明:eoe文章著作权属于作者,受法律保护,转载时请务必以超链接形式附带如下信息原文作者: 沐雨Silence原文地址: http://my.eoe.cn/824176/archive/21974.html
0 0
原创粉丝点击