2011-01-14 67 views
1

如何讓畫廊控件一次滾動一個圖像?還有什麼是使這些圖像連續循環的好方法?我嘗試重寫onFling,根本不起作用。Gallery一次滾動一個圖像

這使圖像移動一定距離但並未真正實現「真正的分頁」。

@Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 

      // return super.onFling(e1, e2, velocityX, velocityY); 
      int kEvent; 
       if(isScrollingLeft(e1, e2)){ //Check if scrolling left 
       kEvent = KeyEvent.KEYCODE_DPAD_LEFT; 
       } 
       else{ //Otherwise scrolling right 
       kEvent = KeyEvent.KEYCODE_DPAD_RIGHT; 
       } 
       onKeyDown(kEvent, null); 
       return true; 
     } 
     private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){ 
       return e2.getX() > e1.getX(); 
      } 

回答

9

我創建了新的控件,將其命名爲CustomGallery並從Gallery中擴展它。在自定義畫廊我放置以下內容:

@Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     return super.onFling(e1, e2, 0, velocityY); 
     } 

在我的活動中,我使用CustomGallery而不是Gallery。這工作。有一件事,我們從2.2改爲2.3(薑餅)。在我嘗試覆蓋onFling之前,它並不適用於我。所以我懷疑這也與操作系統的版本有關。

+2

難道你不認爲如果你有333個意見是關於時間谷歌開始在這裏更直接的解決方案工作? :) – dropsOfJupiter 2011-05-10 04:34:09

3

這一直在工作。在所有版本上不會失敗。

private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) { 
    return e2.getX() < e1.getX(); 
} 

private boolean isScrollingRight(MotionEvent e1, MotionEvent e2) { 
    return e2.getX() > e1.getX(); 
} 

@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
     float velocityY) { 
    boolean leftScroll = isScrollingLeft(e1, e2); 
    boolean rightScroll = isScrollingRight(e1, e2); 

    if (rightScroll) { 
     if (getSelectedItemPosition() != 0)    
      setSelection(getSelectedItemPosition() - 1, true); 
    } else if (leftScroll) { 

     if (getSelectedItemPosition() != getCount() - 1) 
      setSelection(getSelectedItemPosition() + 1, true); 
    } 
    return true; 
} 
+1

你可以給一個示例項目,這是非常緊迫的 – VenomVendor 2012-01-27 09:08:33

5

Aniket Awati的解決方案對我來說最合適。不過,我會建議進行一項改進,以避免在某些情況下捲動兩件物品。

int mSelection = 0; 

@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
     float velocityY) { 
    boolean leftScroll = isScrollingLeft(e1, e2); 
    boolean rightScroll = isScrollingRight(e1, e2); 

    if (rightScroll) { 
     if (mSelection != 0)    
      setSelection(--mSelection, true); 
    } else if (leftScroll) { 

     if (mSelection != getCount() - 1) 
      setSelection(++mSelection, true); 
    } 
    return false; 
} 
相關問題