Android HorizontalScrollView 如何实现滚动状态监听

Android HorizontalScrollView 如何实现滚动状态监听 ?

虽然HorizontalScrollView 本身有自带的onScrollChanged() 回调,但是此回调只会通知滚动改变的距离,并不能实时监控滚动的状态。

    /**
     * This is called in response to an internal scroll in this view (i.e., the
     * view scrolled its own contents). This is typically as a result of
     * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
     * called.
     *
     * @param l Current horizontal scroll origin.
     * @param t Current vertical scroll origin.
     * @param oldl Previous horizontal scroll origin.
     * @param oldt Previous vertical scroll origin.
     */
    protected void onScrollChanged(int l, int t, int oldl, int oldt)

下面是一种比较流行的实现滚动状态监听的方法:即在一定间隔轮询检测 getScrollX(),判断其是否改变,从而得出滚动状态。

//##################################################################################################
//#############################  HorizontalScrollView实现滚动状态监听 ################################
//##################################################################################################
public class MyHorizontalScrollView extends HorizontalScrollView {

    private static final String TAG = "MyHorizontalScrollView";

    private Handler mHandler;

    private ScrollViewListener mScrollViewListener;

    /**
     * 滚动状态:
     * IDLE=滚动停止
     * TOUCH_SCROLL=手指拖动滚动
     * FLING=滚动
     */
    enum ScrollType{IDLE,TOUCH_SCROLL,FLING};

    /**
     * 记录当前滚动的距离
     */
    private int currentX = -9999999;

    /**
     * 当前滚动状态
     */
    private ScrollType scrollType = ScrollType.IDLE;

    public interface ScrollViewListener {  
        void onScrollChanged(ScrollType scrollType);  
    }  

    public MyHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mHandler = new Handler();
        mScrollViewListener = new ScrollViewListener() {
            @Override
            public void onScrollChanged(ScrollType scrollType) {
                //TODO
            }
        });
    }

    /**
     * 滚动监听runnable
     */
    private Runnable scrollRunnable = new Runnable() {
        @Override
        public void run() {
            if (getScrollX()==currentX) {
                //滚动停止,取消监听线程
                scrollType = ScrollType.IDLE;
                if (scrollViewListener!=null) {
                    scrollViewListener.onScrollChanged(scrollType);
                }
                mHandler.removeCallbacks(this);
                return;
            } else { 
                //手指离开屏幕,但是view还在滚动
                scrollType = ScrollType.FLING;
                if(scrollViewListener!=null){
                    scrollViewListener.onScrollChanged(scrollType);
                }
            }
            currentX = getScrollX();
            //滚动监听间隔:milliseconds
            mHandler.postDelayed(this, 50);
        }
    };

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
        case MotionEvent.ACTION_MOVE:
            this.scrollType = ScrollType.TOUCH_SCROLL;
            scrollViewListener.onScrollChanged(scrollType);
            mHandler.removeCallbacks(scrollRunnable);
            break;
        case MotionEvent.ACTION_UP:
            mHandler.post(scrollRunnable);
            break;
        }
        return super.onTouchEvent(ev);
    }
}
   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值