我正在尝试实现滑动检测以更改我的ViewFlipper
. 我使用了下面显示的代码,从那里复制。
虽然onTouch
在触摸 时正确触发,但永远不会ViewFlipper
触发onFling 事件。
有人可以帮忙吗?检测滑动的最佳方法是什么?
谢谢
public class HomeActivity extends Activity {
ViewFlipper viewFlipper;
private GestureDetector gestureDetector;
private Animation slideLeftIn, slideLeftOut, slideRightIn, slideRightOut;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
LinearLayout home = (LinearLayout) findViewById(R.id.home);
// Gesture detection
gestureDetector = new GestureDetector(new MyGestureDetector());
// Info popup
ImageView btn_open_popup = (ImageView) findViewById(R.id.btn_popup);
slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
// Compute size according to density
float density = getResources().getDisplayMetrics().density;
final int width = (int) (295.0f * density);
final int height = (int) (307.0f * density);
btn_open_popup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) HomeActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popup_container = inflater.inflate(R.layout.popup_container,null, false);
final PopupWindow pw = new PopupWindow(popup_container, width, height, true);
pw.showAtLocation(findViewById(R.id.home), Gravity.CENTER, 0,0);
ImageView btn_close_popup = (ImageView) popup_container.findViewById(R.id.btn_close_popup);
btn_close_popup.setAlpha(120);
btn_close_popup.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pw.dismiss();
}
});
// set swipe listener to viewFlipper
viewFlipper=(ViewFlipper)popup_container.findViewById(R.id.popup_flipper);
viewFlipper.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
} else {
return false;
}
}
});
}
});
}
class MyGestureDetector extends SimpleOnGestureListener {
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;
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
System.out.println(" in onFling() :: ");
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
viewFlipper.setInAnimation(slideRightIn);
viewFlipper.setOutAnimation(slideLeftOut);
viewFlipper.showNext();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
viewFlipper.setInAnimation(slideLeftIn);
viewFlipper.setOutAnimation(slideRightOut);
viewFlipper.showPrevious();
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}
}