2011-01-14 7 views
1

一度に1つの画像をスクロールするギャラリーコントロールを作成する方法は?また、それらの画像の連続ループを作る良い方法は何ですか?私はonFlingをオーバーライドしようとしましたが、まったく動作しません。ギャラリー一度に1つの画像をスクロール

イメージをある距離だけ移動させますが、実際には「真のページング」を実装しません。

@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という新しいコントロールを作成し、ギャラリーから拡張しました。私の活動で

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

私が代わりにギャラリーのCustomGalleryを使用しています:カスタムギャラリーでは、私は次のように配置しました。これは機能します。 1つは、2.2から2.3(ジンジャーブレッド)に移動しました。私がonFlingをオーバーライドしようとする前に、私のためにはうまくいかなかった。だから私はこれもOSのバージョンと関係があると思っています。

+2

Googleが333回の表示を取得した場合、Googleがここでより簡単な解決策を開始するとは思わないでしょうか? :) – dropsOfJupiter

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

私は非常に緊急のプロジェクトの例、PLZを与えることはできますか? – VenomVendor

5

Aniket Awatiのソリューションが私にとって最適です。しかし、私はいくつかのケースでスクロールした2つのアイテムの存在を避けるために改善を提案します。

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; 
} 
関連する問題