欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android監(jiān)聽(tīng)ScrollView滑動(dòng)距離的簡(jiǎn)單處理

 更新時(shí)間:2022年02月16日 13:51:03   作者:輝son  
這篇文章主要為大家詳細(xì)介紹了Android監(jiān)聽(tīng)ScrollView滑動(dòng)距離的簡(jiǎn)單處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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){
? ? ? ? ? ? }
? ? ? ? });

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論