如何检查Horizo​​ntalScrollView滚动状态? [英] How to check HorizontalScrollView scroll state?

查看:485
本文介绍了如何检查Horizo​​ntalScrollView滚动状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要访问 Horizo​​ntalScrollView 中的滚动状态。

I need to access the state of scrolling in a HorizontalScrollView. How is it possible?

horizontalScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {

            @Override
            public void onScrollChanged() {

               // .. some code need here
            }
        });


推荐答案

使用scrollview,您可以像下面那样创建自定义想想你也可以创建一个像这样的自定义horizo​​ntalScrollView?

With scrollview you can create a customization like below, i think you can also create a custom horizontalScrollView like that?

public class ScrollViewWithListener extends ScrollView{

    private boolean mCurrentlyTouching;
    private boolean mCurrentlyFling;

    public interface ScrollViewListener {
        public void onScrollChanged(ScrollViewWithListener scrollView, int x, int y, int oldx, int oldy);
        public void onEndScroll();
     }

    private ScrollViewListener scrollViewListener = null;

    public ScrollViewWithListener(Context context) {
        super(context);
    }

    public ScrollViewWithListener(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScrollViewWithListener(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
       this.scrollViewListener = scrollViewListener;
    }

    @Override
    public void fling(int velocityY) {
        super.fling(velocityY);
        mCurrentlyFling = true;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
        }

       if (Math.abs(t - oldt) < 2 || t >= getMeasuredHeight() || t == 0) {
           if(!mCurrentlyTouching){
                if (scrollViewListener != null) {
                   Log.d("SCROLL WITH LISTENER", "-- OnEndScroll");
                   scrollViewListener.onEndScroll();
               }
           }
          mCurrentlyFling = false;
       }

    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
       switch (ev.getAction()) {
      case MotionEvent.ACTION_DOWN:
           mCurrentlyTouching = true;
           break;
      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_CANCEL:
          mCurrentlyTouching = false;
          if(!mCurrentlyFling){
               if (scrollViewListener != null) {
                  Log.d("SCROLL WITH LISTENER", "-- OnEndScroll");
                  scrollViewListener.onEndScroll();
               }
           }
          break;

      default:
         break;
        }
        return super.onTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
       case MotionEvent.ACTION_DOWN:
           mCurrentlyTouching = true;
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            mCurrentlyTouching = false;
            if(!mCurrentlyFling){
               if (scrollViewListener != null) {
                 Log.d("SCROLL WITH LISTENER", "-- OnEndScroll");
                   scrollViewListener.onEndScroll();
               }
           }
            break;

        default:
            break;
       }
       return super.onInterceptTouchEvent(ev);
    }
}

这篇关于如何检查Horizo​​ntalScrollView滚动状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆