android horizontalscrollview 滑动事件,ScrollView的滑动监听(以HorizontalScrollView为例)
发布日期:2021-06-24 10:54:06 浏览次数:3 分类:技术文章

本文共 1549 字,大约阅读时间需要 5 分钟。

ScrollView不能像其他组件一样使用onScrollChanged()方法是因为它用protected封装了

protected void onScrollChanged(int x, int y, int oldx, int oldy);

想要实现监听需要简单自定义组件.

1:自定义组件

public class ObservableScrollView extends HorizontalScrollView {

private ScrollViewListener scrollViewListener = 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 setScrollViewListener(ScrollViewListener scrollViewListener) {

this.scrollViewListener = scrollViewListener;

}

@Override

protected void onScrollChanged(int x, int y, int oldx, int oldy) {

super.onScrollChanged(x, y, oldx, oldy);

if (scrollViewListener != null) {

scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);

}

}

}

接口:

public interface ScrollViewListener {

void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);

}

2:使用监听

horizontalScrollView.setScrollViewListener(new ScrollViewListener() {//滑动监听,获取图片

@Override

public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {

int scrollX = scrollView.getScrollX();

int width = scrollView.getWidth();

int scrollViewMeasuredWidth = holder.imageSL.getChildAt(0).getMeasuredWidth();

if ((scrollX + width) == scrollViewMeasuredWidth) {

/ System.out.println("滑动到了底部 scrollY=" + scrollX + "height=" + width + "scrollViewMeasuredHeight=" + scrollViewMeasuredWidth);

}

}

});

转载地址:https://blog.csdn.net/weixin_32306771/article/details/117682897 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:win7自定义html为桌面,Win7系统自定义桌面主题的方法
下一篇:iphone通讯录 android,3个方法,教你如何快速而又有效的将联系人从iPhone转移到安卓...

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月04日 03时19分47秒