SoFunction
Updated on 2025-03-11

Example of using ViewFlipper for gesture switching in Android

This article describes the method of using ViewFlipper to switch gestures in Android, and is shared with you for your reference. The specific implementation steps are as follows:

First, define a ViewFlipper in the layout's xml file:

Copy the codeThe code is as follows:
<?xml version="1.0" encoding="utf-8"?> 
<ViewFlipper xmlns:andro 
        android: 
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent" 
        > 
         
    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        > 
         
        <TextView 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            android:background="#FF0000" 
            /> 
             
    </LinearLayout> 
     
    <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        > 
         
        <TextView 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            android:background="#00FF00" 
            /> 
             
    </LinearLayout> 
     
</ViewFlipper>

Declare a new GestureDetector, rewrite its onFling() function, judge gestures in this function, here is a horizontal drag:

Copy the codeThe code is as follows:
public class MyGestureDetector extends SimpleOnGestureListener 

    private static final int SWIPE_MIN_DISTANCE = 120; 
    private static final int SWIPE_MAX_OFF_PATH = 250; 
    private static final int SWIPE_THRESHOLD_VELOCITY = 200; 
     
    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2,  
                           float velocityX, float velocityY) 
    { 
        try 
        { 
               if ((() - ()) > SWIPE_MAX_OFF_PATH) 
               { 
                   return false; 
               } 
               if(() - () > SWIPE_MIN_DISTANCE &&  
                  (velocityX) > SWIPE_THRESHOLD_VELOCITY) 
               { 
                (slideLeftIn); 
                   (slideLeftOut); 
                (); 
               } 
               else  
                if (() - () > SWIPE_MIN_DISTANCE &&  
                    (velocityX) > SWIPE_THRESHOLD_VELOCITY) 
                { 
                    (slideRightIn); 
                    (slideRightOut); 
                    (); 
                } 
           } 
        catch (Exception e) 
        { 
             
           } 
           return false; 
    } 
}

Note that override the onTouchEvent() function in Activity:

Copy the codeThe code is as follows:
private ViewFlipper viewFlipper; 
 
private Animation slideLeftIn; 
private Animation slideLeftOut; 
private Animation slideRightIn; 
private Animation slideRightOut; 
 
private GestureDetector gestureDetector; 
@Override 
public void onCreate(Bundle savedInstanceState) 

    (savedInstanceState); 
    setContentView(); 
     
    viewFlipper = (ViewFlipper)findViewById(); 
       slideLeftIn = (this, .slide_left_in); 
       slideLeftOut = (this, .slide_left_out); 
       slideRightIn = (this, .slide_right_in); 
       slideRightOut = (this, .slide_right_out); 
        
       gestureDetector = new GestureDetector(new MyGestureDetector()); 
        
       new () 
       { 
           public boolean onTouch(View v, MotionEvent event) 
           { 
               if ((event)) 
               { 
                   return true; 
               } 
               return false; 
           } 
       }; 

 
@Override 
public boolean onTouchEvent(MotionEvent event) 

    if ((event)) 
    { 
        return true; 
    } 
    else 
    { 
        return false; 
    } 
}

Finally, create a new anim folder in the res folder to store the animation files to switch between views:

1.slide_left_in.xml

Copy the codeThe code is as follows:
<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:andro> 
    <translate 
        android:fromXDelta="100%p"  
        android:toXDelta="0"  
        android:duration="500" 
        /> 
    <alpha 
        android:fromAlpha="0.0"  
        android:toAlpha="1.0" 
        android:duration="500" 
        /> 
</set>

2.slide_left_out.xml

Copy the codeThe code is as follows:
<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:andro> 
    <translate 
        android:fromXDelta="0"  
        android:toXDelta="-100%p"  
        android:duration="500" 
        /> 
    <alpha 
        android:fromAlpha="1.0"  
        android:toAlpha="0.0"  
        android:duration="500" 
        /> 
</set>

3.slide_right_in.xml

Copy the codeThe code is as follows:
<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:andro> 
    <translate  
        android:fromXDelta="-100%p"  
        android:toXDelta="0"  
        android:duration="500" 
        /> 
    <alpha 
        android:fromAlpha="0.0"  
        android:toAlpha="1.0" 
        android:duration="500" 
        /> 
</set>

4.slide_right_out.xml

Copy the codeThe code is as follows:
<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:andro> 
    <translate  
        android:fromXDelta="0"  
        android:toXDelta="100%p"  
        android:duration="500" 
        /> 
    <alpha 
        android:fromAlpha="1.0"  
        android:toAlpha="0.0" 
        android:duration="500" 
        /> 
</set>

I hope this article will be helpful to everyone's Android programming design