1

我有一个画廊,每个当前项目都与显示尺寸相匹配。我希望当用户尝试将视图“扔”到任何一侧时,而不是自动滚动,我只想转到下一个视图。

我怎样才能做到这一点?

4

1 回答 1

1

检查这个片段。它不使用滚动常量,而是依靠 scolling 键事件来处理不同的分辨率。

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;  
}

这段代码属于一个galleryView模型,它扩展了Gallery,它的布局看起来像这样

<?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>
于 2011-08-22T19:02:35.857 回答