随笔 - 84  文章 - 0  评论 - 0  阅读 - 30788

阶段一: 深入了解view

时间 2022.08.31

学习view, 以及view 在项目中的使用; 总结 练习, 加复盘!!

view 与 viewGroup

 

view 滑动: 

(1) onlayout()  |   offsetLeftAndRight()  offsetTopAndBottom  | LayoutParams  | scrollTo()  scrollBy() | Scroller;

(2) Scroller 源码分析;

 

view的事件分发机制

(1) 源码解析activity的构成:

Activity phoneWindow  decroview contentView

 

(2) 事件分发

dispatchTouchEvent(): 事件分发

onInterceptTouchEvent():  拦截事件

onTouchEvent():  处理是按

 Event: action_down, action_move, action_up

分发是由Activity 向 底下的view 进行传递, 如果某一部分确定消耗该Event, 则Event 不分发;

如果一直到最后一个view都不处理, 则再往上抛; 

1
2
3
4
5
6
7
8
9
public boolean dispatchTouchEvent(MotionEvent ev) {
        boolean consume = false;//事件是否被消费
        if (onInterceptTouchEvent(ev)){//调用onInterceptTouchEvent判断是否拦截事件
            consume = onTouchEvent(ev);//如果拦截则调用自身的onTouchEvent方法
        }else{
            consume = child.dispatchTouchEvent(ev);//不拦截调用子View的dispatchTouchEvent方法
        }
        return consume;//返回值表示事件是否被消费,true事件终止,false调用父View的onTouchEvent方法
    }

 核心理解:  判断当前view 是否拦截, 如果拦截则事件不分发, 然后确定当前view是否处理

                                                 如果不拦截, 则继续分发给子view;

所以总体看 : dispatchTouchEvent 是事件分发的起点, 具体的执行是由onInterceptTouchEvent()  和  onTouchEvent() 来实现的 

可参考该文章: https://www.51cto.com/article/678248.html 

 

 

知识点:

View.onTouchEvent()  方法作用:  处理屏幕触摸事件

handle touch screen motion events; 

 

View.invalidate() 方法作用:  导致view 重绘

Invalidate the whole view. If the view is visible, onDraw(Canvas) will be called at some point in the future.

 

View.computeScroll() 方法作用:

Called by a parent to request that a child update its values for mScrollX and mScrollY if necessary.

This will typically be done if the child is animating a scroll using a Scroller object.

 

Scroller. computeScrollOffset() 方法作用: 计算最新位置

Call this when you want to know the new location. If it returns true, the animation is not yet finished

 

posted on   黄山一叶  阅读(97)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示