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;
	}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值