Android監(jiān)聽ScrollView滑動距離的簡單處理
本文實例為大家分享了Android監(jiān)聽ScrollView滑動距離的具體方法,供大家參考,具體內(nèi)容如下
使用ScrollView時,有時候我們需要要獲取它滑動的距離,Android的API給我們提供了設(shè)置監(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
點進(jìn)去看下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){ ? ? ? ? ? ? } ? ? ? ? });
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android使用SkinManager實現(xiàn)換膚功能的示例
本篇文章主要介紹了android使用SkinManager實現(xiàn)換膚功能的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02Android五種隱藏狀態(tài)欄和標(biāo)題欄的方法
這篇文章主要介紹了Android五種隱藏狀態(tài)欄和標(biāo)題欄的方法的相關(guān)資料,需要的朋友可以參考下2017-05-05Android軟件啟動動畫及動畫結(jié)束后跳轉(zhuǎn)的實現(xiàn)方法
這篇文章主要介紹了Android軟件啟動動畫及動畫結(jié)束后跳轉(zhuǎn)的實現(xiàn)方法,實例分析了Android圖片播放及定時器的相關(guān)使用技巧,非常具有使用價值,需要的朋友可以參考下2015-10-10Android自定義View實現(xiàn)價格區(qū)間選擇控件
這篇文章主要為大家詳細(xì)介紹了Android如何利用自定義View實現(xiàn)價格區(qū)間選擇控件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-11-11Jetpack?Compose?Canvas繪制超詳細(xì)介紹
Canvas?是允許您在屏幕上指定區(qū)域并在此區(qū)域上執(zhí)行繪制的組件。您必須使用修飾符指定尺寸,無論是通過Modifier.size修飾符指定確切尺寸,還是通過Modifier.fillMaxSize,ColumnScope.weight等相對于父級指定精確尺寸。如果父級包裝了此子級,則僅必須指定確切尺寸2022-10-10