豆瓣学习 - 实现左右滑动的ViewFlipper

有时候会用到ViewFliper实现简单的左右滑屏切换

package cn.itcast.viewflipper;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class DemoActivity extends Activity {
ViewFlipper vf;
//手势识别的帮助类 
GestureDetector  mGestureDetector ;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        vf = (ViewFlipper) this.findViewById(R.id.vf);
        TextView tv1 = new TextView(this);
        tv1.setText("第一个界面");
        ImageView iv2 = new ImageView(this);
        iv2.setImageResource(R.drawable.ic_launcher);
        TextView tv3 = new TextView(this);
        tv3.setText("第三个界面");
        
        vf.addView(tv1);
        vf.addView(iv2);
        vf.addView(tv3);
        mGestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener(){

			@Override
			public boolean onFling(MotionEvent e1, MotionEvent e2,
					float velocityX, float velocityY) {
			
				if( Math.abs(e1.getY()-e2.getY()) > 100){
					//垂直方向变化过大 无效事件
					return false;
				}
				if( e1.getX() - e2.getX() > 100 && Math.abs(velocityX)>100)
				{
					
					showpre();
					return false;
				}
				if( e2.getX() - e1.getX() > 100 && Math.abs(velocityX)>100 )
				{
					
					shownext();
					
					return false;
				}
				
				return super.onFling(e1, e2, velocityX, velocityY);
			}
        	
        	
        	
        });
        
        
    }
    public void pre(View view){
    	showpre();
    }
	private void showpre() {
		vf.showPrevious();
    	AlphaAnimation inaa = new AlphaAnimation(0.0f, 1.0f);
    	inaa.setDuration(1000);
    	vf.setInAnimation(inaa);
    	AlphaAnimation outaa = new AlphaAnimation(1.0f, 0.0f);
    	outaa.setDuration(1000);
    	vf.setOutAnimation(outaa);
	}
    public void next(View view){
    	shownext();
    }
	private void shownext() {
		vf.showNext();
    	AlphaAnimation inaa = new AlphaAnimation(0.0f, 1.0f);
    	inaa.setDuration(1000);
    	vf.setInAnimation(inaa);
    	AlphaAnimation outaa = new AlphaAnimation(1.0f, 0.0f);
    	outaa.setDuration(1000);
    	vf.setOutAnimation(outaa);
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		// 把当前屏幕触摸的事件 传递个手势识别器 
		mGestureDetector.onTouchEvent(event);
		//有的时候 消费掉 触摸事件 
		// 控制当前的事件 是否让系统的框架 默认处理 如果不交由其处理 则返回true 否则 他的父view还会执行这个event 造成错误
		return super.onTouchEvent(event);
	}
    
    
    
}

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="pre"
            android:text="上一个" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="next"
            android:text="下一个" />
    </LinearLayout>

    <ViewFlipper
        android:id="@+id/vf"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ViewFlipper>

</LinearLayout>


posted @ 2012-12-01 21:49  sfshine  阅读(235)  评论(0编辑  收藏  举报