2011-12-21 171 views
6

我在android市場(ViewPager)中有水平滾動頁面。ViewPager內部的水平滾動視圖

我的問題是,我想有一個水平滾動視圖在他們的一些圖像嗎?

現在,我在我的視圖中看到一個小滾動,然後整個頁面滾動。

謝謝你的時間!

+0

watch [this](http://stackoverflow.com/questions/7774642/scroll-webview-horizo​​ntally-inside-a-viewpager)answer。但是,在該答案中,您可能需要擴展Horizo​​ntalScrollingView,而不是定製WebView。 – grine4ka 2013-10-30 09:19:54

回答

0
<Linearlayout> 
    <linearlayout> 
     <scrollView> 
      <ImageView1></ImageView> 
      <ImageView2></....> 
     </scrollView> 
    </Linearlayout> 
    <EditText> 
</Linearlayout> 

在上述情況下,scrollVIew僅適用於兩個圖像,不適用於edittext。

在其他情況下:

<Linearlayout> 
    <linearlayout> 
     <scrollView> 
      here listVIew with Images using listView adapter 
     </scrollView> 
    </Linearlayout> 
    <EditText> 
</Linearlayout> 

這裏滾動視圖是隻適用於ListView控件。

18

您需要擴展HorizontalScrollView並截獲觸摸事件。什麼工作對我來說是下面的示例:

public class MyScrollView extends HorizontalScrollView { 

    public MyScrollView(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); 
    } 
} 

然後,而不是在你的佈局XML使用HorizontalScrollView,你需要使用這個自定義視圖。

什麼幫助我得到這個解決方案是this post

+0

它適用於我,其他解決方案不!但它似乎禁用了自定義滾動視圖內的按鈕上的點擊處理。任何想法? – user1365836 2012-11-06 11:19:04

+0

@ user1365836是啊......這可能有點麻煩......我所做的是使用getHitRect,並找到確切的按鈕,然後調用它的onClick函數 – Muzikant 2012-11-07 02:29:44

+6

如果你想做的唯一的事情是允許滾動,但仍然允許用戶點擊裏面的元素,你只需要控制onInterceptTouchEvent()中的所有內容。 https://gist.github.com/brandondenney/b8ddd655664eb295129d – Brandon 2013-02-07 17:29:30

1

我已經重新設計一個解決方案,終於找到了實現它以同樣的方式它已經在Gmail上做了一個非常簡單的方法:在Horizo​​ntalScrollView將滾動,直到它達到一個它的邊緣。然後在下一個滾動時,整個頁面將滾動。

所需的全部內容是覆蓋Horizo​​ntalScrollView來檢查滾動方向與邊緣,並確保內容可以實際滾動。

@Override 
public boolean onTouchEvent(MotionEvent ev) 
{ 
    if (no_scrolling) 
     return false; 

    // Standard behavior 
    // 
    return super.onTouchEvent(ev); 
} 

boolean no_scrolling = false; 
float old_x, old_y; 
@Override 
public boolean onInterceptTouchEvent(MotionEvent ev) 
{ 
    int action = ev.getActionMasked(); 
    Log.d(at_data.TAG, "HSV scroll intercept: " + String.format("0x%08x", action)); 

    if (action == MotionEvent.ACTION_DOWN) 
    { 
     old_x = ev.getX(); 
     old_y = ev.getY(); 
     no_scrolling = false; 

    } 
    else if (action == MotionEvent.ACTION_MOVE) 
    { 
     float dx = ev.getX() - old_x; 
     float dy = ev.getY() - old_y; 

     if (Math.abs(dx) > Math.abs(dy) && dx != 0) 
     { 
      View hsvChild = getChildAt(0); 
      int childW = hsvChild.getWidth(); 
      int W = getWidth(); 

      Log.d(at_data.TAG, "HSV " + childW + " > " + W + " ? dx = " + dx + " dy = " + dy); 
      if (childW > W) 
      { 
       int scrollx = getScrollX(); 
       if ((dx < 0 && scrollx + W >= childW) || (dx > 0 && scrollx <= 0)) 
       { 
        Log.d(at_data.TAG, "HSV Wider: on edge already"); 
        no_scrolling = true; 
        return false; 
       } 
       else 
       { 
        Log.d(at_data.TAG, "HSV Wider: can scroll"); 
        no_scrolling = false; 
       } 
      } 
      else 
      { 
       Log.d(at_data.TAG, "HSV cannot scroll in desired direction"); 
       no_scrolling = true; 
      } 
     } 
    } 

    // Standard behavior 
    // 
    return super.onInterceptTouchEvent(ev); 
}