我了解到動畫效果可以運用到布局中,能實現onclicklistener, ontouchlistener事件。
但是不了解當向左邊或右邊滑動布局時,哪個事件會發生。
previousButton.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
viewFlipper.setInAnimation(MainActivity.this,R.anim.view_transition_in_right);
viewFlipper.setOutAnimation(MainActivity.this,R.anim.view_transition_out_right);
viewFlipper.showPrevious();
}
});
}
我不想實現onclicklistener事件。如果我把當前的布局觸摸並且滑動到左邊,下一個布局就會在當前的視圖中。
如何處理這個問題啊?
讓activity實現onGestureListener
然後在activity添加
private GestureDetector gestureScanner;
gestureScanner = new GestureDetector(this);
private static final int SWIPE_MIN_DISTANCE = 100;
private static final int SWIPE_THRESHOLD_VELOCITY = 100;
重寫onTouchEvent(MotionEvent event)
@Override
public boolean onTouchEvent(MotionEvent event) {
return gestureScanner.onTouchEvent(event);
}
最後也要重寫onFling方法
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
/* on scroll to the next page */
if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY
) {
//Ur code goes here
}
/* on scroll to the previous page */
else if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY
) {
//ur code goes here.
}
return false;
}