網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了Android監(jiān)聽ScrollView滑動距離的具體方法,供大家參考,具體內(nèi)容如下
使用ScrollView時,有時候我們需要要獲取它滑動的距離,Android的API給我們提供了設置監(jiān)聽的方法:
scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { ? ? ? ? ? ? } ? ? ? ? });
很遺憾的是:Call requires API 23
點進去看下View里面的OnScrollChangeListener在哪個方法里面監(jiān)聽位置:
/** ? ? ?* This is called in response to an internal scroll in this view (i.e., the ? ? ?* view scrolled its own contents). This is typically as a result of ? ? ?* {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been ? ? ?* called. ? ? ?* ? ? ?* @param l Current horizontal scroll origin. ? ? ?* @param t Current vertical scroll origin. ? ? ?* @param oldl Previous horizontal scroll origin. ? ? ?* @param oldt Previous vertical scroll origin. ? ? ?*/ ? ? protected void onScrollChanged(int l, int t, int oldl, int oldt) { ? ? ? ? notifySubtreeAccessibilityStateChangedIfNeeded(); ? ? ? ? if (AccessibilityManager.getInstance(mContext).isEnabled()) { ? ? ? ? ? ? postSendViewScrolledAccessibilityEventCallback(); ? ? ? ? } ? ? ? ? mBackgroundSizeChanged = true; ? ? ? ? if (mForegroundInfo != null) { ? ? ? ? ? ? mForegroundInfo.mBoundsChanged = true; ? ? ? ? } ? ? ? ? final AttachInfo ai = mAttachInfo; ? ? ? ? if (ai != null) { ? ? ? ? ? ? ai.mViewScrollChanged = true; ? ? ? ? } ? ? ? ? if (mListenerInfo != null && mListenerInfo.mOnScrollChangeListener != null) { ? ? ? ? ? ? mListenerInfo.mOnScrollChangeListener.onScrollChange(this, l, t, oldl, oldt); ? ? ? ? } ? ? }
一看其實實現(xiàn)不難,不就是自定義個ScrollView, 里面多寫個監(jiān)聽, 實現(xiàn)如下:
import android.content.Context; import android.util.AttributeSet; import android.widget.ScrollView; /** ?* Created by hucanhui on 16/7/28. ?*/ public class ObservableScrollView extends ScrollView{ ? ? private OnScollChangedListener onScollChangedListener = null; ? ? public ObservableScrollView(Context context) { ? ? ? ? super(context); ? ? } ? ? public ObservableScrollView(Context context, AttributeSet attrs, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int defStyle) { ? ? ? ? super(context, attrs, defStyle); ? ? } ? ? public ObservableScrollView(Context context, AttributeSet attrs) { ? ? ? ? super(context, attrs); ? ? } ? ? public void setOnScollChangedListener(OnScollChangedListener onScollChangedListener) { ? ? ? ? this.onScollChangedListener = onScollChangedListener; ? ? } ? ? @Override ? ? protected void onScrollChanged(int x, int y, int oldx, int oldy) { ? ? ? ? super.onScrollChanged(x, y, oldx, oldy); ? ? ? ? if (onScollChangedListener != null) { ? ? ? ? ? ? onScollChangedListener.onScrollChanged(this, x, y, oldx, oldy); ? ? ? ? } ? ? } ? ? public interface OnScollChangedListener { ? ? ? ? void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy); ? ? } }
使用簡單:
scrollView.setOnScollChangedListener(new ObservableScrollView.OnScollChangedListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy){ ? ? ? ? ? ? } ? ? ? ? });
原文鏈接:https://blog.csdn.net/u014798175/article/details/51345196
相關推薦
- 2022-12-11 Dart多個future隊列完成加入順序關系及原子性論證_Dart
- 2022-12-14 React?Fiber與調(diào)和深入分析_React
- 2024-01-10 Maven導入org.apache.commons.lang3.StringUtils
- 2022-11-29 C#交換兩個變量值的幾種方法總結(jié)_C#教程
- 2022-04-09 iOS浮點類型精度問題的原因與解決辦法_IOS
- 2022-05-17 獲取當前機器注冊到nacos上的機器ip
- 2022-10-22 BroadcastReceiver靜態(tài)注冊案例詳解_Android
- 2022-12-08 C語言程序如何求學生總成績和平均成績_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支