Android Viewflipper Simple Usage Resolution _android

Source: Internet
Author: User
Tags stub

Viewflipper and Viewpager are very similar, are a view container. You can add more than one view inside, but Viewpager can toggle view by sliding left and right, while Viewflipper does not have this function, so it is necessary to listen for gestures on it. It is more convenient that it can add view without using the adapter, so it is more convenient.


First, define the control in the layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:orientation=" vertical "
  android:layout_width=" fill_parent "
  android:layout_" height= "Fill_parent"
  android:background= "#ffffff" >
  
  <viewflipper android:id= "@+id/viewflipper01"
    android:layout_width= "match_parent" 
    android:layout_height= "match_parent"/>
  
</linearlayout >

2. Find and add view in code

  @Override public
  void OnCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.main);

    detector = new Gesturedetector (this);
    Flipper = (viewflipper) This.findviewbyid (R.ID.VIEWFLIPPER01);

    Flipper.addview (Addtextview ("Page 1"))//Add view to Flipper, add 5 textview
    Flipper.addview (Addtextview ("Page 2" ));
    Flipper.addview (Addtextview ("Page 3"));
    Flipper.addview (Addtextview ("page 4"));
    Flipper.addview (Addtextview ("page 5"));
  }


/**
   * @param text
   * @return A TextView object
  /Private View Addtextview (String text) {
    TextView TV = new TextView (this);
    Tv.settext (text);
    Tv.settextsize ();
    Tv.settextcolor (0xff000000);
    Tv.setgravity (gravity.center);
    return TV;
  }

3. Switch view by Touch monitor gesture

@Override Public
  Boolean onfling (motionevent E1, motionevent E2, float Velocityx,
      float velocityy) {
    if ( E1.getx ()-E2.getx () > () {
      this.flipper.setInAnimation animationutils.loadanimation (This, r.anim.push_ left_in));
      This.flipper.setOutAnimation (Animationutils.loadanimation (this, r.anim.push_left_out));
      This.flipper.showNext ()//switch to Next view return
      true;
    } else if (E1.getx ()-E2.getx () < -120) {
      This.flipper.setInAnimation (Animationutils.loadanimation (this, r.anim.push_right_in));
      This.flipper.setOutAnimation (Animationutils.loadanimation (this, r.anim.push_right_out));
      This.flipper.showPrevious ();
      return true;
    }
    return false;
  }

Here is the complete code for the activity:

Package com.xu81.testflip;
Import android.app.Activity;
Import Android.app.ActivityManager.RecentTaskInfo;
Import Android.os.Bundle;
Import Android.view.GestureDetector;
Import android.view.Gravity;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.GestureDetector.OnGestureListener;
Import Android.view.animation.AnimationUtils;
Import Android.widget.TextView;

Import Android.widget.ViewFlipper;

  public class Testflip extends activity implements Ongesturelistener {private viewflipper flipper;

  Private Gesturedetector detector; /** called the activity is a.
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

    Setcontentview (R.layout.main);
    detector = new Gesturedetector (this);

    Flipper = (viewflipper) This.findviewbyid (R.ID.VIEWFLIPPER01);
    Flipper.addview (Addtextview ("Page 1"))//Add view to Flipper, add 5 textview Flipper.addview ("Page 2") here; FlippEr.addview (Addtextview ("Page 3"));
    Flipper.addview (Addtextview ("page 4"));
  Flipper.addview (Addtextview ("page 5")); /** * @param text * @return A TextView object/private View Addtextview (String text) {TextView TV = new
    TextView (this);
    Tv.settext (text);
    Tv.settextsize (40);
    Tv.settextcolor (0xff000000);
    Tv.setgravity (Gravity.center);
  return TV;
  @Override public boolean ontouchevent (Motionevent event) {return this.detector.onTouchEvent (event);
  @Override public boolean Ondown (Motionevent e) {//TODO auto-generated method stub return false; @Override public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy) {if ( E1.getx ()-E2.getx () > () {this.flipper.setInAnimation animationutils.loadanimation (this, r.anim.push_left_in
      ));
      This.flipper.setOutAnimation (Animationutils.loadanimation (this, r.anim.push_left_out)); This.flipper.showNext ()//Switch toNext view return true; else if (E1.getx ()-E2.getx () < -120) {this.flipper.setInAnimation (this, R.anim.
      push_right_in));
      This.flipper.setOutAnimation (Animationutils.loadanimation (this, r.anim.push_right_out));
      This.flipper.showPrevious ();
    return true;
  return false; @Override public void onlongpress (Motionevent e) {//TODO automatically generated method stub} @Override public Boolean OnS
  Croll (motionevent E1, motionevent E2, float Distancex, float distancey) {//TODO automatically generated method stub return false; @Override public void onshowpress (Motionevent e) {//TODO automatically generated method stub} @Override public Boolean on
  Singletapup (motionevent e) {//TODO automatically generated method stub return false;

 }


}

Animated file:

Push_left_in.xml

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
  <translate android:fromxdelta=" "100%p" android:toxdelta= "0"
    android:duration= "/>"
  <alpha android:fromalpha= "0.1" android:toalpha= "1.0" android:duration= "/>"
</set>

Push_left_out.xml

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
  <translate android:fromxdelta=" 0 "android:toxdelta=" -100%p "
    android:duration=" 500 "/ >
  <alpha android:fromalpha= "1.0" android:toalpha= "0.1" android:duration=/> "</set"
>

Push_right_in.xml

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
  <translate android:fromxdelta=" " -100%p" android:toxdelta= "0"
    android:duration= "/>"
  <alpha android:fromalpha= "0.1" android:toalpha= "1.0" android:duration= "/>"
</set>

Push_right_out.xml

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
  <translate android:fromxdelta=" "0" android:toxdelta= "100%p" android:duration= "/>"
  <alpha android:fromalpha= "1.0" android:toalpha= "0.1" android:duration= "/>"
</set>

SOURCE Download: Http://xiazai.jb51.net/201609/yuanma/AndroidViewFlip (jb51.net). rar

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.