Android_ListView_onTouchEvent源码分析

2024-02-06 22:08

本文主要是介绍Android_ListView_onTouchEvent源码分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android ListView  onTouchEvent源码简单分析,在看代码之前先来看下代码结构图


1.onTouchEvent源码

    @Overridepublic boolean onTouchEvent(MotionEvent ev) {if (!isEnabled()) {// A disabled view that is clickable still consumes the touch// events, it just doesn't respond to them.return isClickable() || isLongClickable();}// AbsListView 绘制与控制手指快速滚动的辅助类if (mFastScroller != null) {boolean intercepted = mFastScroller.onTouchEvent(ev);if (intercepted) {return true;}}final int action = ev.getAction();View v;int deltaY;// 获取触摸滚动时的速率if (mVelocityTracker == null) {mVelocityTracker = VelocityTracker.obtain();}mVelocityTracker.addMovement(ev);// ListView触屏事件主要从ACTION操作划分switch (action & MotionEvent.ACTION_MASK) {case MotionEvent.ACTION_DOWN: {......break;}case MotionEvent.ACTION_MOVE: {......break;}case MotionEvent.ACTION_UP: {switch (mTouchMode) {case TOUCH_MODE_DOWN:case TOUCH_MODE_TAP:case TOUCH_MODE_DONE_WAITING:......mTouchMode = TOUCH_MODE_REST;break;case TOUCH_MODE_SCROLL:......break;}setPressed(false);// Need to redraw since we probably aren't drawing the selector anymoreinvalidate();final Handler handler = getHandler();if (handler != null) {handler.removeCallbacks(mPendingCheckForLongPress);}if (mVelocityTracker != null) {mVelocityTracker.recycle();mVelocityTracker = null;}mActivePointerId = INVALID_POINTER;if (PROFILE_SCROLLING) {if (mScrollProfilingStarted) {Debug.stopMethodTracing();mScrollProfilingStarted = false;}}break;}case MotionEvent.ACTION_CANCEL: {mTouchMode = TOUCH_MODE_REST;......break;}case MotionEvent.ACTION_POINTER_UP: {......break;}}return true;}

2.ACTION_DOWN,主要是CheckForTap

        case MotionEvent.ACTION_DOWN: {mActivePointerId = ev.getPointerId(0);final int x = (int) ev.getX();final int y = (int) ev.getY();// 手指按下时x,y坐标,获取当前选中的itemint motionPosition = pointToPosition(x, y);// 如果ListView 数据未发生变化if (!mDataChanged) {if ((mTouchMode != TOUCH_MODE_FLING) && (motionPosition >= 0)&& (getAdapter().isEnabled(motionPosition))) {// User clicked on an actual view (and was not stopping a fling). It might be a// click or a scroll. Assume it is a click until proven otherwisemTouchMode = TOUCH_MODE_DOWN;// TAP机制,主要是用于去除手指点击抖动// 使Item处于按下状态if (mPendingCheckForTap == null) {mPendingCheckForTap = new CheckForTap();}// 添加到消息队列并延时ViewConfiguration.getTapTimeout()执行此runnablepostDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());} else {if (ev.getEdgeFlags() != 0 && motionPosition < 0) {// If we couldn't find a view to click on, but the down event was touching// the edge, we will bail out and try again. This allows the edge correcting// code in ViewRoot to try to find a nearby view to selectreturn false;}// 之前处于Fling模式if (mTouchMode == TOUCH_MODE_FLING) {// Stopped a fling. It is a scroll.createScrollingCache();// 更改为scrollmTouchMode = TOUCH_MODE_SCROLL;mMotionCorrection = 0;motionPosition = findMotionRow(y);reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL);}}}// 对于ACTION_MOVE,ACTION_UP会使用的触屏位置信息进行记录if (motionPosition >= 0) {// Remember where the motion event startedv = getChildAt(motionPosition - mFirstPosition);mMotionViewOriginalTop = v.getTop();}mMotionX = x;mMotionY = y;mMotionPosition = motionPosition;mLastY = Integer.MIN_VALUE;break;}

3.ACTION_MOVE,主要是startScrollIfNeeded和trackMotionScroll

        case MotionEvent.ACTION_MOVE: {final int pointerIndex = ev.findPointerIndex(mActivePointerId);final int y = (int) ev.getY(pointerIndex);// 获取y轴当前与前一次的偏移值deltaY = y - mMotionY;switch (mTouchMode) {case TOUCH_MODE_DOWN:case TOUCH_MODE_TAP:case TOUCH_MODE_DONE_WAITING:// 必须移动一段距离后才会执行滚动startScrollIfNeeded(deltaY);break;case TOUCH_MODE_SCROLL:if (PROFILE_SCROLLING) {if (!mScrollProfilingStarted) {Debug.startMethodTracing("AbsListViewScroll");mScrollProfilingStarted = true;}}// 手指移动if (y != mLastY) {deltaY -= mMotionCorrection;int incrementalDeltaY = mLastY != Integer.MIN_VALUE ? y - mLastY : deltaY;// No need to do all this work if we're not going to move anywayboolean atEdge = false;if (incrementalDeltaY != 0) {// 滚动的重要方法,滚动的具体处理就是这里atEdge = trackMotionScroll(deltaY, incrementalDeltaY);}// ListView滚动到边界后不不能再进行移动if (atEdge && getChildCount() > 0) {// Treat this like we're starting a new scroll from the current// position. This will let the user start scrolling back into// content immediately rather than needing to scroll back to the// point where they hit the limit first.int motionPosition = findMotionRow(y);if (motionPosition >= 0) {final View motionView = getChildAt(motionPosition - mFirstPosition);mMotionViewOriginalTop = motionView.getTop();}mMotionY = y;mMotionPosition = motionPosition;invalidate();}// 记录当前Y值,用于下次计算偏移量mLastY = y;}break;}break;}

4.ACTION_UP,主要是PerformClick, mPendingCheckForLongPress, mFlingRunnable

        case MotionEvent.ACTION_UP: {switch (mTouchMode) {case TOUCH_MODE_DOWN:case TOUCH_MODE_TAP:case TOUCH_MODE_DONE_WAITING:final int motionPosition = mMotionPosition;final View child = getChildAt(motionPosition - mFirstPosition);if (child != null && !child.hasFocusable()) {// 清理Item按下状态if (mTouchMode != TOUCH_MODE_DOWN) {child.setPressed(false);}// 执行Item Clickif (mPerformClick == null) {mPerformClick = new PerformClick();}final AbsListView.PerformClick performClick = mPerformClick;performClick.mChild = child;performClick.mClickMotionPosition = motionPosition;performClick.rememberWindowAttachCount();mResurrectToPosition = motionPosition;if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) {final Handler handler = getHandler();if (handler != null) {// 清理tap或者long press长按handler.removeCallbacks(mTouchMode == TOUCH_MODE_DOWN ?mPendingCheckForTap : mPendingCheckForLongPress);}mLayoutMode = LAYOUT_NORMAL;if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {mTouchMode = TOUCH_MODE_TAP;setSelectedPositionInt(mMotionPosition);layoutChildren();child.setPressed(true);positionSelector(child);setPressed(true);if (mSelector != null) {Drawable d = mSelector.getCurrent();if (d != null && d instanceof TransitionDrawable) {((TransitionDrawable) d).resetTransition();}}postDelayed(new Runnable() {public void run() {child.setPressed(false);setPressed(false);if (!mDataChanged) {post(performClick);}mTouchMode = TOUCH_MODE_REST;}}, ViewConfiguration.getPressedStateDuration());} else {mTouchMode = TOUCH_MODE_REST;}return true;} else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {post(performClick);}}mTouchMode = TOUCH_MODE_REST;break;case TOUCH_MODE_SCROLL:final int childCount = getChildCount();if (childCount > 0) {if (mFirstPosition == 0 && getChildAt(0).getTop() >= mListPadding.top &&mFirstPosition + childCount < mItemCount &&getChildAt(childCount - 1).getBottom() <=getHeight() - mListPadding.bottom) {mTouchMode = TOUCH_MODE_REST;reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);} else {// 是否执行ListView Scroll Flingfinal VelocityTracker velocityTracker = mVelocityTracker;velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);// 获取当前触屏滚动速率final int initialVelocity = (int) velocityTracker.getYVelocity(mActivePointerId);if (Math.abs(initialVelocity) > mMinimumVelocity) {if (mFlingRunnable == null) {mFlingRunnable = new FlingRunnable();}reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);// 执行ListView 快速滚动(Scroll Fling)mFlingRunnable.start(-initialVelocity);} else {mTouchMode = TOUCH_MODE_REST;reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);}}} else {mTouchMode = TOUCH_MODE_REST;reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);}break;}setPressed(false);// Need to redraw since we probably aren't drawing the selector anymoreinvalidate();final Handler handler = getHandler();if (handler != null) {handler.removeCallbacks(mPendingCheckForLongPress);}if (mVelocityTracker != null) {mVelocityTracker.recycle();mVelocityTracker = null;}mActivePointerId = INVALID_POINTER;if (PROFILE_SCROLLING) {if (mScrollProfilingStarted) {Debug.stopMethodTracing();mScrollProfilingStarted = false;}}break;}

这篇关于Android_ListView_onTouchEvent源码分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/685690

相关文章

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

关于MyISAM和InnoDB对比分析

《关于MyISAM和InnoDB对比分析》:本文主要介绍关于MyISAM和InnoDB对比分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录开篇:从交通规则看存储引擎选择理解存储引擎的基本概念技术原理对比1. 事务支持:ACID的守护者2. 锁机制:并发控制的艺

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛

github打不开的问题分析及解决

《github打不开的问题分析及解决》:本文主要介绍github打不开的问题分析及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、找到github.com域名解析的ip地址二、找到github.global.ssl.fastly.net网址解析的ip地址三

Mysql的主从同步/复制的原理分析

《Mysql的主从同步/复制的原理分析》:本文主要介绍Mysql的主从同步/复制的原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录为什么要主从同步?mysql主从同步架构有哪些?Mysql主从复制的原理/整体流程级联复制架构为什么好?Mysql主从复制注意

java -jar命令运行 jar包时运行外部依赖jar包的场景分析

《java-jar命令运行jar包时运行外部依赖jar包的场景分析》:本文主要介绍java-jar命令运行jar包时运行外部依赖jar包的场景分析,本文给大家介绍的非常详细,对大家的学习或工作... 目录Java -jar命令运行 jar包时如何运行外部依赖jar包场景:解决:方法一、启动参数添加: -Xb

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

Apache 高级配置实战之从连接保持到日志分析的完整指南

《Apache高级配置实战之从连接保持到日志分析的完整指南》本文带你从连接保持优化开始,一路走到访问控制和日志管理,最后用AWStats来分析网站数据,对Apache配置日志分析相关知识感兴趣的朋友... 目录Apache 高级配置实战:从连接保持到日志分析的完整指南前言 一、Apache 连接保持 - 性