Я хочу добавить действие броска в LinearLayout.Для этой цели я использовал следующий код:
public class NewsActivity extends Activity {
..........
...........
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
@Override
public void onCreate(Bundle savedInstanceState) {
.....................
.....................
.....................
LinearLayout newDeailsBlock = (LinearLayout) findViewById(R.id.newdeailsblock);
// Gesture detection
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
};
newDeailsBlock.setOnTouchListener(gestureListener);
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
Log.d("move","Swipe test");
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
// right to left swipe
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.d("move","Left Swipe");
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(NewsActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
Log.d("move","Right Swipe");
}
} catch (Exception e) {
// nothing
}
return false;
}
}
}
Но он вообще не работает, и amd gestureDetector.onTouchEvent(event)
возвращает false.В чем проблема в моем коде?