实现监听NestedScrollView拖拽、惯性滑动、滑动停止、滑动到顶部和底部

本文主要是介绍实现监听NestedScrollView拖拽、惯性滑动、滑动停止、滑动到顶部和底部,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

运行实时监听日志:

在这里插入图片描述

因为在开发中经常会需要在滚动的各种状态下处理一些UI界面功能,但是系统又没有提供实时监听拖拽、惯性滑动、滑动停止、滑动到顶部和底部等功能。那怎么办,只能自己去实现这些功能。

  • 滚动的几种状态
    /*** 滚动状态*/public enum ScrollState{DRAG,      // 拖拽中SCROLLING, // 正在滚动IDLE       // 已停止}
  • 回调方法
 public interface AddScrollChangeListener{/*** 滚动监听* @param scrollX* @param scrollY* @param oldScrollX* @param oldScrollY*/void onScrollChange( int scrollX, int scrollY, int oldScrollX, int oldScrollY);/*** 滚动状态** @param state*/void onScrollState(ScrollState state);}

重写 public boolean onTouchEvent(MotionEvent ev)实现 监听拖拽、监听惯性滑动、监听滑动停止

  • 监听拖拽
  • 监听惯性滑动
  • 监听滑动停止
@Overridepublic boolean onTouchEvent(MotionEvent ev) {switch (ev.getAction()){case MotionEvent.ACTION_DOWN:case MotionEvent.ACTION_MOVE:isStart = false ;LogUtils.LOG_V("[NewNestedScrollView]->DRAG 拖拽中");if (addScrollChangeListener!=null){addScrollChangeListener.onScrollState(ScrollState.DRAG);}break;case MotionEvent.ACTION_CANCEL:case MotionEvent.ACTION_OUTSIDE:case MotionEvent.ACTION_UP:isStart = true ;start();break;}return super.onTouchEvent(ev);}/**** 开始计算是否停止还是正在滚性滑动**/private void start() {new  Thread(new Runnable() {@Overridepublic void run() {while (isStart){if ((System.currentTimeMillis() - lastTime)>50){int newScrollY = getScrollY();lastTime = System.currentTimeMillis();if (newScrollY - lastScrollY == 0){isStart = false ;LogUtils.LOG_V("[NewNestedScrollView]->IDLE 停止滚动");handler.post(new Runnable() {@Overridepublic void run() {if (addScrollChangeListener!=null){addScrollChangeListener.onScrollState(ScrollState.IDLE);}}});}else {handler.post(new Runnable() {@Overridepublic void run() {LogUtils.LOG_V("[NewNestedScrollView]->SCROLLING 正在滚动中");if (isStart&&addScrollChangeListener!=null){addScrollChangeListener.onScrollState(ScrollState.SCROLLING );}}});}lastScrollY = newScrollY;}}}}).start();}
  • 监听滑动到顶部
@Overridepublic void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {if (getScrollY()<=0){LogUtils.LOG_V("[NewNestedScrollView]->onScrollChange = top");top = true ;}else {top = false ;}}
  • 监听底部 (首先需要知道整个滚动内容的高度和当前滚动控件view的高度)从写onMeasure()测量测试高度
@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);totalHeight = 0 ;int count = getChildCount();for (int i =0 ;i < count ;i++){View view = getChildAt(i);totalHeight += view.getMeasuredHeight();}viewHeight = getHeight() ;}@Overridepublic void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {if (totalHeight>viewHeight && (totalHeight - viewHeight) == scrollY){LogUtils.LOG_V("[NewNestedScrollView]->onScrollChange = bottom");bottom = true ;}else {bottom = false ;}}

完整代码列子

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;public class NewNestedScrollView extends NestedScrollView implements NestedScrollView.OnScrollChangeListener {/****/private AddScrollChangeListener addScrollChangeListener;/*** 滚动状态*/public enum ScrollState{DRAG,      // 拖拽中SCROLLING, // 正在滚动IDLE       // 已停止}/**** 记录上一次滑动**/private int lastScrollY ;/***/private boolean isStart = false ;/*** 上一次记录的时间*/private long lastTime ;private Handler handler;/*** 整個滾動内容高度**/public int totalHeight = 0 ;/**** 当前view的高度**/public int viewHeight = 0 ;/*** 是否滚动到底了*/private boolean bottom = false;/*** 是否滚动在顶部** @param context*/private boolean top = false ;public NewNestedScrollView(@NonNull Context context) {this(context,null);}public NewNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {this(context, attrs,0);}public NewNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);setOnScrollChangeListener(this);handler = new Handler(context.getMainLooper());}@Overridepublic void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {/*实时滚动回调*/if (addScrollChangeListener!=null){addScrollChangeListener.onScrollChange( scrollX,  scrollY,  oldScrollX,  oldScrollY);}if (totalHeight>viewHeight && (totalHeight - viewHeight) == scrollY){LogUtils.LOG_V("[NewNestedScrollView]->onScrollChange = bottom");bottom = true ;}else {bottom = false ;}if (getScrollY()<=0){LogUtils.LOG_V("[NewNestedScrollView]->onScrollChange = top");top = true ;}else {top = false ;}}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);totalHeight = 0 ;int count = getChildCount();for (int i =0 ;i < count ;i++){View view = getChildAt(i);totalHeight += view.getMeasuredHeight();}viewHeight = getHeight() ;}/*** 是否动到底* @return*/public boolean isBottom(){return bottom;}/*** 是否滚动到了 顶部** @return*/public boolean isTop(){return top;}@Overridepublic boolean onTouchEvent(MotionEvent ev) {switch (ev.getAction()){case MotionEvent.ACTION_DOWN:case MotionEvent.ACTION_MOVE:isStart = false ;LogUtils.LOG_V("[NewNestedScrollView]->DRAG 拖拽中");if (addScrollChangeListener!=null){addScrollChangeListener.onScrollState(ScrollState.DRAG);}break;case MotionEvent.ACTION_CANCEL:case MotionEvent.ACTION_OUTSIDE:case MotionEvent.ACTION_UP:isStart = true ;start();break;}return super.onTouchEvent(ev);}/**** 开始计算是否停止还是正在滚性滑动**/private void start() {new  Thread(new Runnable() {@Overridepublic void run() {/*** 表示已停止*/while (isStart){if ((System.currentTimeMillis() - lastTime)>50){int newScrollY = getScrollY();lastTime = System.currentTimeMillis();if (newScrollY - lastScrollY == 0){isStart = false ;LogUtils.LOG_V("[NewNestedScrollView]->IDLE 停止滚动");handler.post(new Runnable() {@Overridepublic void run() {if (addScrollChangeListener!=null){addScrollChangeListener.onScrollState(ScrollState.IDLE);}}});}else {handler.post(new Runnable() {@Overridepublic void run() {LogUtils.LOG_V("[NewNestedScrollView]->SCROLLING 正在滚动中");if (isStart&&addScrollChangeListener!=null){addScrollChangeListener.onScrollState(ScrollState.SCROLLING );}}});}lastScrollY = newScrollY;}}}}).start();}/*** 设置监听** @param addScrollChangeListener* @return*/public NewNestedScrollView addScrollChangeListener(AddScrollChangeListener addScrollChangeListener) {this.addScrollChangeListener =  addScrollChangeListener;return this ;}public interface AddScrollChangeListener{/*** 滚动监听* @param scrollX* @param scrollY* @param oldScrollX* @param oldScrollY*/void onScrollChange( int scrollX, int scrollY, int oldScrollX, int oldScrollY);/*** 滚动状态** @param state*/void onScrollState(ScrollState state);}
}

如何使用

private class NewOnScroll implements NewNestedScrollView.AddScrollChangeListener {@Overridepublic void onScrollChange(int scrollX, int scrollY, int oldScrollX, int oldScrollY) {}@Overridepublic void onScrollState(NewNestedScrollView.ScrollState state) {MainActivity activity = (MainActivity) getActivity();switch (state){case DRAG:case SCROLLING:activity.anim(Util.screenWidth()-Util.dp2Px(30));activity.alpha(0.4f);break;case IDLE:activity.anim(Util.screenWidth()-Util.dp2Px(70));activity.alpha(1f);break;}}}

这篇关于实现监听NestedScrollView拖拽、惯性滑动、滑动停止、滑动到顶部和底部的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

PyCharm中配置PyQt的实现步骤

《PyCharm中配置PyQt的实现步骤》PyCharm是JetBrains推出的一款强大的PythonIDE,结合PyQt可以进行pythion高效开发桌面GUI应用程序,本文就来介绍一下PyCha... 目录1. 安装China编程PyQt1.PyQt 核心组件2. 基础 PyQt 应用程序结构3. 使用 Q

Python实现批量提取BLF文件时间戳

《Python实现批量提取BLF文件时间戳》BLF(BinaryLoggingFormat)作为Vector公司推出的CAN总线数据记录格式,被广泛用于存储车辆通信数据,本文将使用Python轻松提取... 目录一、为什么需要批量处理 BLF 文件二、核心代码解析:从文件遍历到数据导出1. 环境准备与依赖库

linux下shell脚本启动jar包实现过程

《linux下shell脚本启动jar包实现过程》确保APP_NAME和LOG_FILE位于目录内,首次启动前需手动创建log文件夹,否则报错,此为个人经验,供参考,欢迎支持脚本之家... 目录linux下shell脚本启动jar包样例1样例2总结linux下shell脚本启动jar包样例1#!/bin

go动态限制并发数量的实现示例

《go动态限制并发数量的实现示例》本文主要介绍了Go并发控制方法,通过带缓冲通道和第三方库实现并发数量限制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录带有缓冲大小的通道使用第三方库其他控制并发的方法因为go从语言层面支持并发,所以面试百分百会问到

Go语言并发之通知退出机制的实现

《Go语言并发之通知退出机制的实现》本文主要介绍了Go语言并发之通知退出机制的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、通知退出机制1.1 进程/main函数退出1.2 通过channel退出1.3 通过cont

Python实现PDF按页分割的技术指南

《Python实现PDF按页分割的技术指南》PDF文件处理是日常工作中的常见需求,特别是当我们需要将大型PDF文档拆分为多个部分时,下面我们就来看看如何使用Python创建一个灵活的PDF分割工具吧... 目录需求分析技术方案工具选择安装依赖完整代码实现使用说明基本用法示例命令输出示例技术亮点实际应用场景扩

C#监听txt文档获取新数据方式

《C#监听txt文档获取新数据方式》文章介绍通过监听txt文件获取最新数据,并实现开机自启动、禁用窗口关闭按钮、阻止Ctrl+C中断及防止程序退出等功能,代码整合于主函数中,供参考学习... 目录前言一、监听txt文档增加数据二、其他功能1. 设置开机自启动2. 禁止控制台窗口关闭按钮3. 阻止Ctrl +

java如何实现高并发场景下三级缓存的数据一致性

《java如何实现高并发场景下三级缓存的数据一致性》这篇文章主要为大家详细介绍了java如何实现高并发场景下三级缓存的数据一致性,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 下面代码是一个使用Java和Redisson实现的三级缓存服务,主要功能包括:1.缓存结构:本地缓存:使