網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Android監(jiān)聽(tīng)ScrollView滑動(dòng)距離的簡(jiǎn)單處理_Android
作者:輝son ? 更新時(shí)間: 2022-04-21 編程語(yǔ)言本文實(shí)例為大家分享了Android監(jiān)聽(tīng)ScrollView滑動(dòng)距離的具體方法,供大家參考,具體內(nèi)容如下
使用ScrollView時(shí),有時(shí)候我們需要要獲取它滑動(dòng)的距離,Android的API給我們提供了設(shè)置監(jiān)聽(tīng)的方法:
scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { ? ? ? ? ? ? } ? ? ? ? });
很遺憾的是:Call requires API 23
點(diǎn)進(jìn)去看下View里面的OnScrollChangeListener在哪個(gè)方法里面監(jiān)聽(tīng)位置:
/** ? ? ?* 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); ? ? ? ? } ? ? }
一看其實(shí)實(shí)現(xiàn)不難,不就是自定義個(gè)ScrollView, 里面多寫(xiě)個(gè)監(jiān)聽(tīng), 實(shí)現(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); ? ? } }
使用簡(jiǎn)單:
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
相關(guān)推薦
- 2022-09-24 opencv實(shí)現(xiàn)圖像傾斜校正_C 語(yǔ)言
- 2022-04-16 統(tǒng)計(jì)C語(yǔ)言二叉樹(shù)中葉子結(jié)點(diǎn)個(gè)數(shù)_C 語(yǔ)言
- 2022-07-19 Linux 性能監(jiān)測(cè)命令
- 2021-12-07 Kotlin基本數(shù)據(jù)類(lèi)型詳解_Android
- 2022-06-17 C語(yǔ)言深入講解函數(shù)參數(shù)的使用_C 語(yǔ)言
- 2023-01-05 Kotlin協(xié)程Channel特點(diǎn)及使用細(xì)節(jié)詳解_Android
- 2022-06-21 Android實(shí)現(xiàn)登陸界面的記住密碼功能_Android
- 2022-12-23 C++類(lèi)中如何使用定義的類(lèi)型別名_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支