android viewgroup点击变色,android ViewGroup 事件分发

1、ViewGroup跟上一节View一样先看各种情况下的日志然后在根据日志情况来分析源码

1.1 在默认都存在的情况下日志

09-12 06:46:19.390 19085-19085/com.example.chao.viewontouch I/Tag: ViewGroup -> dispatchTouchEvent -> 0

09-12 06:46:19.390 19085-19085/com.example.chao.viewontouch I/Tag: ViewGroup -> onInterceptTouchEvent -> 0

09-12 06:46:19.390 19085-19085/com.example.chao.viewontouch I/Tag: view -> dispatchTouchEvent -> 0

09-12 06:46:19.390 19085-19085/com.example.chao.viewontouch I/Tag: view -> onTouch

09-12 06:46:19.390 19085-19085/com.example.chao.viewontouch I/Tag: view -> onTouchEvent -> 0

09-12 06:46:19.410 19085-19085/com.example.chao.viewontouch I/Tag: ViewGroup -> dispatchTouchEvent -> 2

09-12 06:46:19.410 19085-19085/com.example.chao.viewontouch I/Tag: ViewGroup -> onInterceptTouchEvent -> 2

09-12 06:46:19.410 19085-19085/com.example.chao.viewontouch I/Tag: view -> dispatchTouchEvent -> 2

09-12 06:46:19.410 19085-19085/com.example.chao.viewontouch I/Tag: view -> onTouch

09-12 06:46:19.410 19085-19085/com.example.chao.viewontouch I/Tag: view -> onTouchEvent -> 2

09-12 06:46:19.557 19085-19085/com.example.chao.viewontouch I/Tag: ViewGroup -> dispatchTouchEvent -> 1

09-12 06:46:19.557 19085-19085/com.example.chao.viewontouch I/Tag: ViewGroup -> onInterceptTouchEvent -> 1

09-12 06:46:19.557 19085-19085/com.example.chao.viewontouch I/Tag: view -> dispatchTouchEvent -> 1

09-12 06:46:19.557 19085-19085/com.example.chao.viewontouch I/Tag: view -> onTouch

09-12 06:46:19.558 19085-19085/com.example.chao.viewontouch I/Tag: view -> onTouchEvent -> 1

09-12 06:46:19.561 19085-19085/com.example.chao.viewontouch I/Tag: view -> onClick

1.2 这次是取消onClickListener情况下日志 取消了onClickListener 等于没有消费事件

09-12 06:44:39.744 17254-17254/com.example.chao.viewontouch I/Tag: ViewGroup -> dispatchTouchEvent -> 0

09-12 06:44:39.744 17254-17254/com.example.chao.viewontouch I/Tag: ViewGroup -> onInterceptTouchEvent -> 0

09-12 06:44:39.744 17254-17254/com.example.chao.viewontouch I/Tag: view -> dispatchTouchEvent -> 0

09-12 06:44:39.744 17254-17254/com.example.chao.viewontouch I/Tag: view -> onTouch

09-12 06:44:39.744 17254-17254/com.example.chao.viewontouch I/Tag: view -> onTouchEvent -> 0

09-12 06:44:39.744 17254-17254/com.example.chao.viewontouch I/Tag: ViewGroup -> onTouchEvent ->0

1.3 在view中的onTouchEvent 返回值为true

09-12 06:45:40.509 18253-18253/com.example.chao.viewontouch I/Tag: ViewGroup -> dispatchTouchEvent -> 0

09-12 06:45:40.509 18253-18253/com.example.chao.viewontouch I/Tag: ViewGroup -> onInterceptTouchEvent -> 0

09-12 06:45:40.509 18253-18253/com.example.chao.viewontouch I/Tag: view -> dispatchTouchEvent -> 0

09-12 06:45:40.509 18253-18253/com.example.chao.viewontouch I/Tag: view -> onTouch

09-12 06:45:40.509 18253-18253/com.example.chao.viewontouch I/Tag: view -> onTouchEvent -> 0

09-12 06:45:40.690 18253-18253/com.example.chao.viewontouch I/Tag: ViewGroup -> dispatchTouchEvent -> 2

09-12 06:45:40.690 18253-18253/com.example.chao.viewontouch I/Tag: ViewGroup -> onInterceptTouchEvent -> 2

09-12 06:45:40.690 18253-18253/com.example.chao.viewontouch I/Tag: view -> dispatchTouchEvent -> 2

09-12 06:45:40.690 18253-18253/com.example.chao.viewontouch I/Tag: view -> onTouch

09-12 06:45:40.690 18253-18253/com.example.chao.viewontouch I/Tag: view -> onTouchEvent -> 2

09-12 06:45:40.690 18253-18253/com.example.chao.viewontouch I/Tag: ViewGroup -> dispatchTouchEvent -> 1

09-12 06:45:40.690 18253-18253/com.example.chao.viewontouch I/Tag: ViewGroup -> onInterceptTouchEvent -> 1

09-12 06:45:40.690 18253-18253/com.example.chao.viewontouch I/Tag: view -> dispatchTouchEvent -> 1

09-12 06:45:40.690 18253-18253/com.example.chao.viewontouch I/Tag: view -> onTouch

09-12 06:45:40.690 18253-18253/com.example.chao.viewontouch I/Tag: view -> onTouchEvent -> 1

1.4 在ViewGroup中的onInterceptTouchEvent返回true

09-12 05:51:04.638 21669-21669/com.example.chao.viewontouch I/Tag: ViewGroup -> dispatchTouchEvent -> 0

09-12 05:51:04.638 21669-21669/com.example.chao.viewontouch I/Tag: ViewGroup -> onInterceptTouchEvent -> 0

09-12 05:51:04.638 21669-21669/com.example.chao.viewontouch I/Tag: ViewGroup -> onTouchEvent ->0

接下来第二节来通过源码解析为什么会出现这种情况

首先还是找到ViewGroup的dispatchTouchEvent(ev)

boolean handled = false dispatchTouchEvent 返回值

接下来看这段话 这段话就清除所有的状态 resetTouchState() mFirstTouchTarget 也是在里面置空 mFirstTouchTarget 下面会讲到

if (actionMasked == MotionEvent.ACTION_DOWN) {

// Throw away all previous state when starting a new touch gesture.

// The framework may have dropped the up or cancel event for the previous gesture

// due to an app switch, ANR, or some other state change.

cancelAndClearTouchTargets(ev);

resetTouchState();

}

以下这段代码比较重要 第一次按下的话 actionMasked == MotionEvent.ACTION_DOWN 这个肯定是down事件 所以一定会进入if判断

final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0; 这段话就是判断子View有没有请求父控件不拦截 也就是这段 getParent().requestDisallowInterceptTouchEvent(true);

这里我们也没有调用这个方法 所以intercepted为false就是不拦截继续往下走

final boolean intercepted;

if (actionMasked == MotionEvent.ACTION_DOWN

|| mFirstTouchTarget != null) {

final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;

if (!disallowIntercept) {

intercepted = onInterceptTouchEvent(ev);

ev.setAction(action); // restore action in case it was changed

} else {

intercepted = false;

}

} else {

// There are no touch targets and this action is not an initial down

// so this view group continues to intercept touches.

intercepted = true;

}

接下来这段代码比较长 只取关键代码newTouchTarget = addTouchTarget(child, idBitsToAssign); addTouchTarget 这段对mFirstTouchTarget 和 newTouchTarget赋值

if (actionMasked == MotionEvent.ACTION_DOWN

|| (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN)

|| actionMasked == MotionEvent.ACTION_HOVER_MOVE) {

....

.....

if (newTouchTarget == null && childrenCount != 0) {

....

....

newTouchTarget = addTouchTarget(child, idBitsToAssign);

}

}

整个ViewGroup事件分发的核心就这这下面的代码了

// Dispatch to touch targets.

if (mFirstTouchTarget == null) {

// No touch targets so treat this as an ordinary view.

handled = dispatchTransformedTouchEvent(ev, canceled, null,

TouchTarget.ALL_POINTER_IDS);

} else {

// Dispatch to touch targets, excluding the new touch target if we already

// dispatched to it. Cancel touch targets if necessary.

TouchTarget predecessor = null;

TouchTarget target = mFirstTouchTarget;

while (target != null) {

final TouchTarget next = target.next;

if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) {

handled = true;

} else {

final boolean cancelChild = resetCancelNextUpFlag(target.child)

|| intercepted;

if (dispatchTransformedTouchEvent(ev, cancelChild,

target.child, target.pointerIdBits)) {

handled = true;

}

if (cancelChild) {

if (predecessor == null) {

mFirstTouchTarget = next;

} else {

predecessor.next = next;

}

target.recycle();

target = next;

continue;

}

}

predecessor = target;

target = next;

}

}

经过上面的分析 我们知道 mFirstTouchTarget 是不为空 所以走else的代码块 我们看到 if (dispatchTransformedTouchEvent(ev, cancelChild,target.child, target.pointerIdBits))这里面

只看一部分

if (cancel || oldAction == MotionEvent.ACTION_CANCEL) {

event.setAction(MotionEvent.ACTION_CANCEL);

if (child == null) {

handled = super.dispatchTouchEvent(event);

} else {

handled = child.dispatchTouchEvent(event);

}

event.setAction(oldAction);

return handled;

}

如果说子 View 没有一个地方返回 true ,只会进来一次只会响应 DOWN 事件,代表不需要消费该事件,如果你想响应 MOVE,UP 必须找个地方ture对于ViewGroup来讲,如果你想拦截子 View 的 Touch 事件,可以覆写 onInterceptTouchEvent 返回 true 即可 , 如果说 onInterceptTouchEvent 返回的是 true 会执行该 ViewGroup 的 onTouchEvent 方法 , 如果子 View 没有消费 touch 事件也会调用该 ViewGroup 的 onTouchEvent 方法

通达信行情API是金融数据提供商通达信(TongDaXin)为开发者和金融机构提供的接口服务,用于获取实时及历史的股票、期货、期权等金融市场数据。这个API允许用户在自己的应用程序中集成通达信的数据服务,实现个性化数据分析、交易策略开发等功能。 1. **API基本概念** - **API**:Application Programming Interface,应用程序编程接口,是软件之间交互的一种方式,提供预定义的函数和方法,使得其他软件能够调用特定功能。 - **通达信**:国内知名的金融终端软件提供商,提供股票、期货、基金等市场数据,以及交易服务。 2. **通达信API的功能** - **实时行情**:获取股票、期货、期权等市场的实时报价信息,包括最新价、涨跌额、涨跌幅、成交量等。 - **历史数据**:获取历史交易日的K线数据、分时数据、交易量等信息,支持自定义时间段查询。 - **深度数据**:获取买卖盘口的五档报价和成交量,有助于分析市场买卖意愿。 - **资讯信息**:获取公告、研报、新闻等市场资讯。 - **交易委托**:通过API进行交易下单、撤单等操作,实现自动化交易。 3. **TdxHqApi** - **TdxHqApi** 是通达信行情API的具体实现,它包含了调用通达信数据服务的各种函数和类,如获取股票列表、获取实时行情、获取历史数据等。 - 开发者需要按照API文档的指示,导入TdxHqApi库,然后通过调用相应的函数来获取所需数据。 4. **使用步骤** - **安装**:下载并安装通达信API的SDK,通常包括头文件和动态链接库。 - **初始化**:在代码中实例化API对象,进行连接设置,如服务器地址、端口号等。 - **连接**:连接到通达信服务器,进行身份验证。 - **数据请求**:调用对应的API函数,例如`GetS
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值