如何在Android App中的ListView上使用Gesture Detector

时间:2014-06-03 20:42:54

标签: java android xml listview gesturedetector

我有gesture detector用于在“页面”之间滚动。每个页面都包含一个列表视图。我遇到的问题是我的滑动没有通过ListView被拾取。它们在ListView的上方和下方工作,而不是在它上面,如下图所示。有没有人知道如何使滑动在ListView上工作?谢谢!

App Capture

这是我的活动:

public class MainActivity extends ActionBarActivity {

ListView listView;

TextView test;
ArrayList<Location> arrayOfLocations;
LocationAdapter adapter;
// public static Bitmap bitmap;
Button refresh;
// ProgressBar progress;
ImageView view;

private GestureDetector gestureDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.listView1);

    // test = (TextView) findViewById(R.id.textView2);
    // Construct the data source
    arrayOfLocations = new ArrayList<Location>();

    // Create the adapter to convert the array to views
    adapter = new LocationAdapter(this, arrayOfLocations);
    adapter.add(new Location(null, "Title", "Test", "Test2","Test3"));
    listView.setAdapter(adapter);

    gestureDetector = new GestureDetector(this, new SwipeGestureDetector());

}

 @Override
  public boolean onTouchEvent(MotionEvent event) {
    if (gestureDetector.onTouchEvent(event)) {
      return true;
    }
    return super.onTouchEvent(event);
  }

  private void onLeftSwipe() {
        Toast.makeText(getApplicationContext(), "Swipe!",
                Toast.LENGTH_LONG).show();
  }

  private void onRightSwipe() {
        Toast.makeText(getApplicationContext(), "Swipe!",
                Toast.LENGTH_LONG).show();
  }

  // Private class for gestures
  private class SwipeGestureDetector 
          extends SimpleOnGestureListener {
    // Swipe properties, you can change it to make the swipe 
    // longer or shorter and speed
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 200;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2,
                         float velocityX, float velocityY) {
      try {
        float diffAbs = Math.abs(e1.getY() - e2.getY());
        float diff = e1.getX() - e2.getX();

        if (diffAbs > SWIPE_MAX_OFF_PATH)
          return false;

        // Left swipe
        if (diff > SWIPE_MIN_DISTANCE
        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
           MainActivity.this.onLeftSwipe();

        // Right swipe
        } else if (-diff > SWIPE_MIN_DISTANCE
        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
          MainActivity.this.onRightSwipe();
        }
      } catch (Exception e) {
        Log.e("YourActivity", "Error on gestures");
      }
      return false;
    }
  }


}

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="vertical" >

<ProgressBar
    android:id="@+id/progressbar_loading"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone" >
</ProgressBar>

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#CCCCCC"
    android:padding="10dip"
    android:text="Stands Near You"
    android:textSize="30dip" />

<Button
    android:id="@+id/refresh"
    android:layout_width="wrap_content"
    android:layout_height="26dp"
    android:layout_marginLeft="250dip"
    android:layout_marginTop="-40dip"
    android:background="#FFFFFF"
    android:text="Refresh"
    android:textSize="9sp" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="20dip"
    android:src="@drawable/abc_ab_bottom_transparent_dark_holo" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="15dp"
    android:dividerHeight="10.0sp" >
</ListView>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

尝试实施onInterceptTouchEvent(MotionEvent event)并将您需要的任何功能传递给onTouchEventSource