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

相关文章

python使用Akshare与Streamlit实现股票估值分析教程(图文代码)

《python使用Akshare与Streamlit实现股票估值分析教程(图文代码)》入职测试中的一道题,要求:从Akshare下载某一个股票近十年的财务报表包括,资产负债表,利润表,现金流量表,保存... 目录一、前言二、核心知识点梳理1、Akshare数据获取2、Pandas数据处理3、Matplotl

python panda库从基础到高级操作分析

《pythonpanda库从基础到高级操作分析》本文介绍了Pandas库的核心功能,包括处理结构化数据的Series和DataFrame数据结构,数据读取、清洗、分组聚合、合并、时间序列分析及大数据... 目录1. Pandas 概述2. 基本操作:数据读取与查看3. 索引操作:精准定位数据4. Group

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

MySQL 内存使用率常用分析语句

《MySQL内存使用率常用分析语句》用户整理了MySQL内存占用过高的分析方法,涵盖操作系统层确认及数据库层bufferpool、内存模块差值、线程状态、performance_schema性能数据... 目录一、 OS层二、 DB层1. 全局情况2. 内存占js用详情最近连续遇到mysql内存占用过高导致

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

深度解析Nginx日志分析与499状态码问题解决

《深度解析Nginx日志分析与499状态码问题解决》在Web服务器运维和性能优化过程中,Nginx日志是排查问题的重要依据,本文将围绕Nginx日志分析、499状态码的成因、排查方法及解决方案展开讨论... 目录前言1. Nginx日志基础1.1 Nginx日志存放位置1.2 Nginx日志格式2. 499

Olingo分析和实践之EDM 辅助序列化器详解(最佳实践)

《Olingo分析和实践之EDM辅助序列化器详解(最佳实践)》EDM辅助序列化器是ApacheOlingoOData框架中无需完整EDM模型的智能序列化工具,通过运行时类型推断实现灵活数据转换,适用... 目录概念与定义什么是 EDM 辅助序列化器?核心概念设计目标核心特点1. EDM 信息可选2. 智能类

Olingo分析和实践之OData框架核心组件初始化(关键步骤)

《Olingo分析和实践之OData框架核心组件初始化(关键步骤)》ODataSpringBootService通过初始化OData实例和服务元数据,构建框架核心能力与数据模型结构,实现序列化、URI... 目录概述第一步:OData实例创建1.1 OData.newInstance() 详细分析1.1.1

Olingo分析和实践之ODataImpl详细分析(重要方法详解)

《Olingo分析和实践之ODataImpl详细分析(重要方法详解)》ODataImpl.java是ApacheOlingoOData框架的核心工厂类,负责创建序列化器、反序列化器和处理器等组件,... 目录概述主要职责类结构与继承关系核心功能分析1. 序列化器管理2. 反序列化器管理3. 处理器管理重要方

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种