package com.hll.ViewFlipperTest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
@SuppressLint("NewApi")public classViewFlipperTest extends Activity implements OnTouchListener,
OnGestureListener, OnDoubleTapListener {private ViewFlipper mFlipper; //翻转视图
GestureDetector mGestureDetector; //手势识别
private int mCurrentLayoutState; //当前布局状态
private static final int FLING_MIN_DISTANCE = 2;private static final int FLING_MIN_VELOCITY = 200;
TextView counttv;
Button buttonNext1= null;
Button buttonNext2= null;
ListView lv1= null;
@Overridepublic voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
setListener();
}/** 查找控件*/@SuppressWarnings("deprecation")public voidfindView() {
mFlipper=(ViewFlipper) findViewById(R.id.details);
mFlipper.setLongClickable(true);
mGestureDetector= new GestureDetector(this);
mCurrentLayoutState= 0;
counttv=(TextView) findViewById(R.id.counttv);
buttonNext1=(Button) findViewById(R.id.Button_next1);
buttonNext2=(Button) findViewById(R.id.Button_next2);
lv1=(ListView) findViewById(R.id.list1);
lv1.setAdapter(new HgroupAdapter(this, null, 0));
}public voidsetListener() {
mFlipper.setOnTouchListener(this);
lv1.setOnTouchListener(this);
counttv.setText("9");
buttonNext1.setOnClickListener(newView.OnClickListener() {public voidonClick(View view) {
mFlipper.showNext();
counttv.setText("7");
}
});
buttonNext2.setOnClickListener(newView.OnClickListener() {public voidonClick(View view) {
mFlipper.showNext();
counttv.setText("8");
}
});
}// protectedAnimation inFromRightAnimation() {
Animation inFromRight= newTranslateAnimation(
Animation.RELATIVE_TO_PARENT,+1f,
Animation.RELATIVE_TO_PARENT,0.0f,
Animation.RELATIVE_TO_PARENT,0.0f,
Animation.RELATIVE_TO_PARENT,0.0f);
inFromRight.setDuration(300);
inFromRight.setInterpolator(newAccelerateInterpolator());returninFromRight;
}protectedAnimation outToLeftAnimation() {
Animation outtoLeft= newTranslateAnimation(
Animation.RELATIVE_TO_PARENT,0.0f,
Animation.RELATIVE_TO_PARENT,-1f,
Animation.RELATIVE_TO_PARENT,0.0f,
Animation.RELATIVE_TO_PARENT,0.0f);
outtoLeft.setDuration(300);
outtoLeft.setInterpolator(newAccelerateInterpolator());returnouttoLeft;
}protectedAnimation inFromLeftAnimation() {
Animation inFromLeft= newTranslateAnimation(
Animation.RELATIVE_TO_PARENT,-1.0f,
Animation.RELATIVE_TO_PARENT,0.0f,
Animation.RELATIVE_TO_PARENT,0.0f,
Animation.RELATIVE_TO_PARENT,0.0f);
inFromLeft.setDuration(300);
inFromLeft.setInterpolator(newAccelerateInterpolator());returninFromLeft;
}protectedAnimation outToRightAnimation() {
Animation outtoRight= newTranslateAnimation(
Animation.RELATIVE_TO_PARENT,0.0f,
Animation.RELATIVE_TO_PARENT,+1.0f,
Animation.RELATIVE_TO_PARENT,0.0f,
Animation.RELATIVE_TO_PARENT,0.0f);
outtoRight.setDuration(300);
outtoRight.setInterpolator(newAccelerateInterpolator());returnouttoRight;
}publicboolean onDown(MotionEvent e) {//TODO Auto-generated method stub
return false;
}public boolean onFling(MotionEvent e1, MotionEvent e2, floatvelocityX,floatvelocityY) {if (e1.getX() - e2.getX() >FLING_MIN_DISTANCE&& Math.abs(velocityX) >FLING_MIN_VELOCITY) {
mFlipper.setInAnimation(inFromRightAnimation());
mFlipper.setOutAnimation(outToLeftAnimation());
mFlipper.showNext();
}else if (e2.getX() - e1.getX() >FLING_MIN_DISTANCE&& Math.abs(velocityX) >FLING_MIN_VELOCITY) {
mFlipper.setInAnimation(inFromLeftAnimation());
mFlipper.setOutAnimation(outToRightAnimation());
mFlipper.showPrevious();
}return false;
}public voidonLongPress(MotionEvent e) {//TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "onLongPress", Toast.LENGTH_LONG)
.show();
}public boolean onScroll(MotionEvent e1, MotionEvent e2, floatdistanceX,floatdistanceY) {
Toast.makeText(getApplicationContext(),"onScroll", Toast.LENGTH_LONG)
.show();return false;
}public voidonShowPress(MotionEvent e) {//TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "onShowPress", Toast.LENGTH_LONG)
.show();
}publicboolean onSingleTapUp(MotionEvent e) {//TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "onSingleTapUp", Toast.LENGTH_LONG)
.show();return false;
}public boolean onTouch(View v, MotionEvent event) {return mGestureDetector.onTouchEvent(event);
}publicboolean onDoubleTap(MotionEvent e) {//TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "onDoubleTap", Toast.LENGTH_LONG)
.show();return false;
}publicboolean onDoubleTapEvent(MotionEvent e) {//TODO Auto-generated method stub
return false;
}publicboolean onSingleTapConfirmed(MotionEvent e) {//TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "onSingleTapConfirmed", Toast.LENGTH_LONG)
.show();return false;
}
}