天天看点

ViewFlipper 控制第一页和最后一页

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
	Android:orientation="vertical" Android:layout_width="fill_parent"
	Android:layout_height="fill_parent">
	<ViewFlipper Android:id="@+id/ViewFlipper01"
		Android:layout_width="fill_parent" Android:layout_height="fill_parent">
	</ViewFlipper>
</LinearLayout>  

pushleft_left_in.xml
<?xml version="1.0" encoding="utf-8"?>    
<set xmlns:android="http://schemas.android.com/apk/res/android">    
    <translate    
    android:fromXDelta="100%p"     
    android:toXDelta="0"     
    android:duration="500"/>    
    <alpha    
    android:fromAlpha="0.0"     
    android:toAlpha="1.0"   
    android:duration="500" />    
</set>   

push_left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="-100%p" />
    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
</set>

push_right_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate      
    android:fromXDelta="-100%p"       
    android:toXDelta="0"       
    android:duration="500"/>      
    <alpha      
    android:fromAlpha="0.0"       
    android:toAlpha="1.0"       
    android:duration="500" /> 

</set>

push_right_out.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="500"/>      
    <alpha      
    android:fromAlpha="1.0"       
    android:toAlpha="1.0"     
    android:duration="500" /> 

</set>

package com.src.android.xysj;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ViewFlipper;
/**
 * ViewFlipper的学习 
 * @author Hero_hwl
 * 2012-4-12
 */
public class Flipper extends Activity implements OnGestureListener {
	/** Called when the activity is first created. */
	private ViewFlipper flipper;

	private GestureDetector detector;
	View item01View, item02View, item03View, item04View;
	Button btn_test;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		detector = new GestureDetector(this);
		item01View = (getLayoutInflater().inflate(R.layout.item01, null));
		item02View = (getLayoutInflater().inflate(R.layout.item02, null));
		item03View = (getLayoutInflater().inflate(R.layout.item03, null));
		item04View = (getLayoutInflater().inflate(R.layout.item04, null));
		flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper01);
		flipper.addView(item01View);
		flipper.addView(item02View);
		flipper.addView(item03View);
		flipper.addView(item04View);
		btn_test = (Button) item01View.findViewById(R.id.btn_test);
		btn_test.setOnClickListener(test);
	}

	View.OnClickListener test = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			Log.v("test", "----------------test");

		}
	};
    /**
     * 此方法是flipper 和 GestureDetector  的接口点
     */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return this.detector.onTouchEvent(event);
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if (e1.getX() - e2.getX() > 120) {
			this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_left_in));
			this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_left_out));
			if (flipper.getCurrentView() != item04View) {
				this.flipper.showNext();
			} else {
				Toast.makeText(this, "当前为最后一页", Toast.LENGTH_SHORT).show();
			}
			return true;
		} else if (e1.getX() - e2.getX() < -120) {
			this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_right_in));
			this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_right_out));
			if (flipper.getCurrentView() != item01View) {
				this.flipper.showPrevious();
			} else {
				Toast.makeText(this, "当前为第一页", Toast.LENGTH_SHORT).show();
			}

			return true;
		}
		return false;
	}

}