android HorizontalScrollView實現滾動狀態監聽

來源:互聯網
上載者:User

標籤:android   scrollview   horizontalscrollview   滾動狀態監聽   


網上大部分都是直接調用onScrollChanged(int x, int y, int oldx, int oldy) 這個方法的,實際上只是將這個方法的protected改為public而已,本質上上還是沒有什麼多大的協助,不多說,直接上代碼



package com.dzc.gallery;import android.content.Context;import android.os.Handler;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.widget.HorizontalScrollView;public class ScrollListenerHorizontalScrollView extends HorizontalScrollView{public ScrollListenerHorizontalScrollView(Context context,AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stub}public ScrollListenerHorizontalScrollView(Context context,AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}public ScrollListenerHorizontalScrollView(Context context) {super(context);// TODO Auto-generated constructor stub}public interface ScrollViewListener {        void onScrollChanged(ScrollType scrollType);    }  private Handler mHandler;private ScrollViewListener scrollViewListener;/** * 滾動狀態   IDLE 滾動停止  TOUCH_SCROLL 手指拖動滾動         FLING滾動 * @version XHorizontalScrollViewgallery * @author DZC * @Time  2014-12-7 上午11:06:52 * * */    enum ScrollType{IDLE,TOUCH_SCROLL,FLING};        /**     * 記錄當前滾動的距離     */    private int currentX = -9999999;    /**     * 當前滾動狀態     */    private ScrollType scrollType = ScrollType.IDLE;    /**     * 滾動監聽間隔     */    private int scrollDealy = 50;    /**     * 滾動監聽runnable     */    private Runnable scrollRunnable = new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubif(getScrollX()==currentX){//滾動停止  取消監聽線程Log.d("", "停止滾動");scrollType = ScrollType.IDLE;if(scrollViewListener!=null){scrollViewListener.onScrollChanged(scrollType);}mHandler.removeCallbacks(this);return;}else{//手指離開螢幕    view還在滾動的時候Log.d("", "Fling。。。。。");scrollType = ScrollType.FLING;if(scrollViewListener!=null){scrollViewListener.onScrollChanged(scrollType);}}currentX = getScrollX();mHandler.postDelayed(this, scrollDealy);}};@Overridepublic 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);}/** * 必須先調用這個方法設定Handler  不然會出錯 *  2014-12-7 下午3:55:39  * @author DZC * @return void * @param handler   * @TODO */public void setHandler(Handler handler){this.mHandler = handler;}/** * 設定滾動監聽 *  2014-12-7 下午3:59:51  * @author DZC * @return void * @param listener   * @TODO */public void setOnScrollStateChangedListener(ScrollViewListener listener){this.scrollViewListener = listener;}}


代碼注釋已經寫得我認為很詳細了,不懂的可以留言,說下思路,就是開啟一個線程,每個一定的時間間隔去判斷當前的scrollX是否有改變,要是有改變就說明還在滾動,要是沒有改變就說明已經停止滾動了。

要注意的一點就是使用的時候必須調用setHandler(Handler handler)傳一個在activity建立的Handler進去,不然會出錯。

也許你會懷疑這樣開一個線程會不會很影響效能,其實這點是不用當心的,幾乎不會有影響

android HorizontalScrollView實現滾動狀態監聽

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.