2013-04-15 79 views
9

我有一個ViewPager內的水平滾動視圖。爲了防止當達到了滾動結束時,我使用這個類按照提示在SO被滾動的ViewPager:OnClickListener在自定義滾動視圖內的視圖

public class CustomScrollView extends HorizontalScrollView { 

public CustomScrollView(Context p_context, AttributeSet p_attrs) { 
    super(p_context, p_attrs); 
} 

@Override 
public boolean onInterceptTouchEvent(MotionEvent p_event) { 

    return true; 
} 

@Override 
public boolean onTouchEvent(MotionEvent p_event) { 


    if (p_event.getAction() == MotionEvent.ACTION_MOVE 
      && getParent() != null) { 
     getParent().requestDisallowInterceptTouchEvent(true); 
    } 

    return super.onTouchEvent(p_event); 
} 
} 

onInterCeptTouchEvent似乎消耗任何點擊該View和裏面的一切。當我將Views放入該ScrollView時,它們的OnClickListener將不會被調用。

當我讓onInterceptTouchEvent返回false時,調用OnClickListener s,但ScrollView不能滾動。

如何將可點擊視圖放入ScrollView

編輯:實施Rotem的答案後,onClickListener的作品,但它不僅觸發點擊事件,而且在其他人,如一扔。這怎麼能被阻止?

回答

5

好了,開始一個賞金我發現它是如何工作分鐘後:在onInterceptTouchEvent

return super.onInterceptTouchEvent(p_event); 
+2

如果你只叫超與相同參數,沒有必要重寫方法 – Rotem

+0

確保你讀了[Javadoc中(http://developer.android.com/ reference/android/view/ViewGroup.html#onInterceptTouchEvent%28android.view.MotionEvent%29),它描述了你的問題。正如Rotem所說,你不需要執行這個。 –

11

試着撥打onTouchEvent裏面執行onInterceptTouchEvent然後返回false。

+0

作品,非常感謝你:) – FWeigl

1

下面是一個使用這個問題的答案完整的解決方案。

public class CustomHorizontalScrollView extends HorizontalScrollView { 

     public CustomHorizontalScrollView(Context context) { 
       super(context); 

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

     @Override 
     public boolean onInterceptTouchEvent(MotionEvent ev) { 
       boolean result = super.onInterceptTouchEvent(ev); 
       if(onTouchEvent(ev)) { 
         return result; 
       } else { 
         return false; 
       } 
     } 

     @Override 
     public boolean onTouchEvent(MotionEvent ev) { 
      if (ev.getAction() == MotionEvent.ACTION_MOVE 
        && getParent() != null) { 
       getParent().requestDisallowInterceptTouchEvent(true); 
      } 
       return super.onTouchEvent(ev); 
     } 

}