码迷,mamicode.com
首页 > 其他好文 > 详细

ScrollView的滑动监听(以HorizontalScrollView为例)

时间:2015-01-27 21:56:10      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:horizontalscrollview   滑动监听   

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);
                    }
                }
            });


ScrollView的滑动监听(以HorizontalScrollView为例)

标签:horizontalscrollview   滑动监听   

原文地址:http://blog.csdn.net/pengkv/article/details/43203085

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!