如何检查Horizo​​ntalScrollView滚动状态?

时间:2016-07-31 12:11:29

标签: java android gridview horizontal-scrolling

我需要访问HorizontalScrollView中的滚动状态。怎么可能?

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

            @Override
            public void onScrollChanged() {

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

1 个答案:

答案 0 :(得分:1)

使用scrollview你可以创建如下所示的自定义,我想你也可以像这样创建一个自定义horizo​​ntalScrollView?

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