main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"></TextView>
<RadioGroup
android:id="@+id/type_radio_group"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:checkedButton="@+id/type_departure_radio"
android:background="@color/blue">
<RadioButton
android:id="@+id/type_departure_radio"></RadioButton>
<RadioButton
android:id="@+id/type_arrival_radio"></RadioButton>
</RadioGroup>
<ListView
android:id="@+id/list_punctuality_arrival"
android:layout_width="fill_parent"
android:background="@color/gray"
android:layout_height="wrap_content"></ListView>
<ViewFlipper
android:id="@+id/flipper_punctuality"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</ViewFlipper>
</LinearLayout>
Activity.java
package com.shimly.gesture;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
public class GestureTestActivity extends Activity implements OnTouchListener,
OnGestureListener {
GestureDetector mGestureDetector;
private static final int FLING_MIN_DISTANCE = 50;
private static final int FLING_MIN_VELOCITY = 0;
ListView list_punctuality_arrival;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list_punctuality_arrival = (ListView) findViewById(R.id.list_punctuality_arrival);
mGestureDetector = new GestureDetector(this);
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
ll.setOnTouchListener(this);
ll.setLongClickable(true);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, new String[] { "a", "b"});
list_punctuality_arrival.setAdapter(adapter);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.e("touch", "touch");
return mGestureDetector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
&& Math.abs(velocityX) > FLING_MIN_VELOCITY) {
Toast.makeText(this, "left", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
&& Math.abs(velocityX) > FLING_MIN_VELOCITY) {
Toast.makeText(this, "right", Toast.LENGTH_SHORT).show();
}
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
}
效果是Android ListView不响应OnTouch事件。