In Android application development, we often need to implement the function of the left and right switch views, which usually need to add ImageView to LinearLayout, relativelayout and other layouts. If you only need to show one picture at a time, and you can display the previous or next picture by sliding the screen left and right, you can do so by Viewflipper or Viewswitcher. Swipe gesture detection is implemented primarily through the Motionevent class, but is implemented in this article by the Gesturedetector class.
The Viewflipper class is usually the best choice when we need to slide the display between two or more views. Use Viewflipper to add the appropriate Viewflipper content to the layout file:
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity "> <Viewflipper android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:id= "@+id/viewflipper"Android:layout_alignparenttop= "true"Android:layout_alignparentleft= "true"Android:layout_alignparentstart= "true" > </ViewFlipper></RelativeLayout>
There are two ways to add a child view to Viewflipper: > Add > Programmatic Additions within a layout resource in the following example, in the second way. Next, add the picture to viewflipper programmatically in the activity:
Public classMainactivityextendsappcompatactivity {PrivateViewflipper Viewflipper; PrivateGesturedetector Gesturedetector; int[] Resources ={r.drawable.one, r.drawable.two, R.drawable.three, R.drawable.four, R.drawable.five, r.drawable.six}; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Viewflipper=(Viewflipper) Findviewbyid (r.id.viewflipper); for(inti = 0; i < resources.length; i++) {ImageView ImageView=NewImageView ( This); Imageview.setimageresource (Resources[i]); Viewflipper.addview (ImageView); } }}
At this point, running the app shows you the first picture, but the slide screen doesn't show other pictures. Below, we use the Gesturedetector class to detect the sliding action, first we need to customize a class that inherits the Simpleongesturelistener, in which to determine the direction of the slide and display the corresponding picture:
classCustomgesturedetectorextendsGesturedetector.simpleongesturelistener {@Override Public BooleanOnfling (motionevent E1, motionevent E2,floatVelocityx,floatvelocityy) { if(E1.getx () > E2.getx ()) {//if the x-coordinate of the initial contact is larger than the x-coordinate of the final contact, swipe leftViewflipper.shownext (); } if(E1.getx () < E2.getx ()) {//swipe right if the x-coordinate of the initial contact is smaller than the x-coordinate of the final contactviewflipper.showprevious (); } return Super. Onfling (E1, E2, Velocityx, Velocityy); }}
Finally, through the simple code in the OnCreate () method to initialize the monitoring method:
Newnew gesturedetector (this, customgesturedetector);
and implement Getsturedetector.ontouchevent (event) in the Ontouchevent () callback;
@Override Public Boolean ontouchevent (Motionevent event) { gesturedetector.ontouchevent (event); return Super . Ontouchevent (event);}
In addition, you can add animation effects to the picture toggle by Viewflipper:
Viewflipper.setinanimation (this, Android. r.anim.fade_in); Viewflipper.setinanimation (this, Android. R.anim.fade_out);
If you want your app to automatically switch pictures, you can also set the picture to play and play automatically via Viewflipper:
Viewflipper.setautostart (true); Viewflipper.setflipinterval (2000);
Reference:
http://codetheory.in/android-viewflipper-and-viewswitcher/
An analysis of Android viewflipper usage