sabato 12 gennaio 2013

Android e il Flip tra Activity come su Iphone

Come "flippare" tra due activity con android: semplice, di seguito lascio un esempio di alcune righe da implementare in un app e organizzare il layout. Si è chiaro che su internet ce ne sono di esempi ma questo a me ha funzionato perfettamente con un sdk 4.1.1 (ossia android 2.2).
I due layout sono raccolti nell'oggetto ViewFlipper, nel onCreate aggiungere il setOnTouchListener e utilizzare il dispatchTouchEvent.
Ho utilizzato il RelativeLayout per inserire in basso i banner, ma è opzionale si può utilizzare benissimo un LinearLayout.
Aggiungere la directory "anim" sotto "res" e creare i 4 layout left_in.xml left_out.xml right_in.xml right_out.xml

Sorgente:

public class Example extends Activity{
 private ViewFlipper vf;
 private static final int SWIPE_MIN_DISTANCE = 150;
 private static final int SWIPE_THRESHOLD_VELOCITY = 200;
 
 private final GestureDetector detector = new GestureDetector(
   new MyGestureDetector());
   
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.example);
  mContext = this;
  
  vf = (ViewFlipper)findViewById(R.id.viewflipper_example);
  
  vf.setOnTouchListener(new OnTouchListener() {
         @Override
         public boolean onTouch(final View view, final MotionEvent event) {
          detector.onTouchEvent(event);
             return true;
         }
     });
  
 }

 class MyGestureDetector extends SimpleOnGestureListener {
  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
    float velocityY) {
   try {
    if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
      && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
     vf.setInAnimation(AnimationUtils.loadAnimation(mContext,
       R.anim.left_in));
     vf.setOutAnimation(AnimationUtils.loadAnimation(mContext,
       R.anim.left_out));
     vf.showNext();
     return true;
    } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
      && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
     vf.setInAnimation(AnimationUtils.loadAnimation(mContext,
       R.anim.right_in));
     vf.setOutAnimation(AnimationUtils.loadAnimation(mContext,
       R.anim.right_out));
     vf.showPrevious();
     return true;
    }

   } catch (Exception e) {
    e.printStackTrace();
   }

   return false;
  }
 }
 
 @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        super.dispatchTouchEvent(event);
        return detector.onTouchEvent(event);
    }
}
Layout xml:

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