2011-08-22 25 views
1

我有一個圖庫,每個當前項目都與顯示尺寸相匹配。我希望當用戶試圖向任何一方「拋出」視圖而不是自動滾動時,我想要去下一個視圖。只能在圖庫上通過手勢導航一個視圖

我該怎麼做?

+0

我轉儲'Gallery'並使用'ImageView'和'GestureDetector' 。在刷卡上,更新'ImageView'。 – CommonsWare

+0

我以前只有在自定義視圖中繪製圖像時才使用此方法。但是我必須使用支持添加任何類型內容的View(如支持HTML 5的WebView) –

+0

OK,然後使用'ViewFlipper'和'GestureDetector'。事實上,我有一個'ViewSwiper',可以做到這一點:https://github.com/commonsguy/cwac-viewswiper – CommonsWare

回答

1

查看此片段。它不使用滾動常量,而是依靠滾動鍵事件來處理不同的分辨率。

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

@Override 
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float 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; 
} 

這段代碼屬於延伸畫廊模型圖庫視圖和它的佈局看起來像這樣

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="180dp"> 
    <com.example.android.GalleryView 
     android:id="@+id/gallery" 
     android:layout_width="fill_parent" 
     android:layout_height="180dp" 
     android:gravity="bottom" 
     android:fadingEdge="none"/> 
</LinearLayout> 
+0

完美的,真的很感謝你! –

相關問題