本文实例为大家分享了Android监听ScrollView滑动距离的具体方法,供大家参考,具体内容如下 使用ScrollView时,有时候我们需要要获取它滑动的距离,Android的
本文实例为大家分享了Android监听ScrollView滑动距离的具体方法,供大家参考,具体内容如下
使用ScrollView时,有时候我们需要要获取它滑动的距离,Android的api给我们提供了设置监听的方法:
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在哪个方法里面监听位置:
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);
}
}
一看其实实现不难,不就是自定义个ScrollView, 里面多写个监听, 实现如下:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;
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){
}
});
--结束END--
本文标题: Android监听ScrollView滑动距离的简单处理
本文链接: https://lsjlt.com/news/139398.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0