Android:ViewFlipper实现左右滑动
用ViewFlipper实现左右滑屏操作:
1.注册ViewFilpper
2.滑动动画:
slideLeftOut.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="400"/> </set>
slideRightOut.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="400"/> </set>
3.viewFilpper添加子view:
viewFlipper.addView(mListView);
viewFlipper.addView(mListView1);
viewFlipper.addView(mListView2);
子view监听onTouchEvent事件
拦截子View的onTouchEvent事件
@Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub LogUtil.info(TAG, "---------onTouchEvent----------"); return mGesture.onTouchEvent(event); }
4.监听屏幕手势
//屏幕手势监听类 class GestureListener extends SimpleOnGestureListener { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { try { 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) { viewFlipper.setInAnimation(slideLeftIn); viewFlipper.setOutAnimation(slideLeftOut); viewFlipper.showNext(); return true; } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { viewFlipper.setInAnimation(slideRightIn); viewFlipper.setOutAnimation(slideRightOut); viewFlipper.showPrevious(); return true; } } catch (Exception e) { // nothing e.printStackTrace(); } return false; } }