多Activity滚动切换[ViewFlipper & ActivityGroup]

main.xml

<?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" >

    <ViewFlipper
        android:id="@+id/viewFlipper1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ViewFlipper>

</LinearLayout>

主Activity

package net.spreadst;

import android.app.ActivityGroup;
import android.content.Intent;
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.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;

public class ActivityGroupDemoActivity extends ActivityGroup implements OnTouchListener,
        OnGestureListener {

    private ViewFlipper vf;
    private GestureDetector gd;

    // identify four animation
    private Animation leftIn;
    private Animation leftOut;
    private Animation rightIn;
    private Animation rightOut;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        vf = (ViewFlipper) this.findViewById(R.id.viewFlipper1);
        gd = new GestureDetector(this);

        vf.addView(
                getLocalActivityManager().startActivity("",
                        new Intent(ActivityGroupDemoActivity.this, ActivityOne.class))
                        .getDecorView(), 0);
        vf.addView(
                getLocalActivityManager().startActivity("",
                        new Intent(ActivityGroupDemoActivity.this, ActivityTwo.class))
                        .getDecorView(), 1);

        vf.setDisplayedChild(0);

        vf.setOnTouchListener(this);
        vf.setLongClickable(true); // allow long click so that it can recognize
                                   // gesture

        leftIn = AnimationUtils.loadAnimation(this, R.anim.left_in);
        leftOut = AnimationUtils.loadAnimation(this, R.anim.left_out);
        rightIn = AnimationUtils.loadAnimation(this, R.anim.right_in);
        rightOut = AnimationUtils.loadAnimation(this, R.anim.right_out);
    }
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        // use GestureDetector object to handle motion event
        return gd.onTouchEvent(arg1);
    }
    @Override
    public boolean onDown(MotionEvent arg0) {
        return false;
    }
    @Override
    public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
            float arg3) {
        // arg0 is the first motion event
        // arg1 is the last motion event
        // arg2 is the velocity of x orientation, px/s
        // arg3 is the velocity of y orientation, px/s
        int fling_min_distance = 100;
        int fling_min_velocity = 200;

        if (arg0.getX() - arg1.getX() > fling_min_distance && Math.abs(arg2) > fling_min_velocity) {
            vf.setInAnimation(rightIn);
            vf.setOutAnimation(leftOut);
            vf.showNext();
        }
        else if (arg1.getX() - arg0.getX() > fling_min_distance
                && Math.abs(arg2) > fling_min_velocity) {
            vf.setInAnimation(leftIn);
            vf.setOutAnimation(rightOut);
            vf.showNext();
        }
        return false;
    }
    @Override
    public void onLongPress(MotionEvent e) {
    }
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        return false;
    }
    @Override
    public void onShowPress(MotionEvent e) {
    }
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }
}


你可能感兴趣的:(多Activity滚动切换[ViewFlipper & ActivityGroup])