2014-02-27 48 views
1

我正在使用水平滾動視圖,並且希望在特定情況下隱藏第一個項目。現在我使用on touch監聽器來查看用戶滾動了多少,並查看該第一個項目/視圖是否可見,然後基於某些計算執行scrollTo()。 一切都很好,直到我意識到,這不會照顧用戶剛剛甩手並且水平滾動視圖滾動一段距離的情況。所以,我無法做出正確的計算。因此,我意識到,最好的辦法是檢查第一個項目是否可見,或者只有當水平滾動視圖最終停止滾動時,纔會觸摸或投擲。如何在水平滾動視圖中獲取滾動狀態:Android

但我找不到一種方式,告訴我什麼時候水平滾動條停止滾動,仍然是。

有人能幫我解決這個問題嗎?

謝謝。

回答

5

擴展ScrollView以添加滾動監聽器。然後,您可以使用該自定義View並在偵聽器中接收事件(代碼使用scrollView,但將其轉換爲Horizo​​ntalScrollView應該非常簡單)。

import android.content.Context; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.widget.ScrollView; 

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

然後你用它在你的XML是這樣的:

<com.example.ScrollViewWithListener 
    android:id="@+id/scrollWithListener" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" > 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="TEST" /> 
</com.example.ScrollViewWithListener> 

不要忘記設置要與滾動變化通知監聽器。