Android uses gestures to turn the answering page effect _android

Source: Internet
Author: User
Tags prepare

This example for you to share the Android Answer page paging function, mainly using viewfilpper and gesturedetector to achieve, for your reference, the specific contents are as follows

1. Effect drawing

2. Realize the idea

Gesturedetector the activity's TouchEvent event, and then use Viewfilpper to control the transitions between multiple components using animation. The gesture of an API is not detailed, you can check if you do not know.

3. Steps to Achieve

1), constructs the gesture detection device
2), prepare the data
3), add child controls for Viewfilpper.
4), initialize animation array
5), the activity of the TouchEvent incident into a gesturedetector to deal with
6), realize Onfling method

4. Code implementation

4.1 Layout files

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout 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:orientation= "vertical"
  tools:context= " Com.lidong.demo.view.GestureFilpActivity ">

  <viewflipper
    android:layout_width=" Match_parent "
    android:layout_height= "match_parent"
    android:id= "@+id/viewflipper"/>
</LinearLayout>

4.2 Animated files

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= "/>"
</set>

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= "/>"
</set>

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= "/>"
</set>

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= "/>"
</set>

Implementation of 4.3GestureFilpActivity

Package Com.lidong.demo.view;
Import Android.os.Bundle;
Import Android.view.GestureDetector;
Import android.view.MotionEvent;
Import Android.view.View;
Import android.view.animation.Animation;
Import Android.view.animation.AnimationUtils;
Import Android.widget.AdapterView;
Import Android.widget.ListView;
Import Android.widget.TextView;
Import Android.widget.Toast;

Import Android.widget.ViewFlipper;
Import com.lidong.demo.AppComponent;
Import com.lidong.demo.BaseActivity;
Import COM.LIDONG.DEMO.R;
Import Com.lidong.demo.view.adapter.ChineseMedicineReportAdapter;

Import com.lidong.demo.view.model.Question;
Import java.util.ArrayList;

Import java.util.List; Import Butterknife.
Bind; Import Butterknife.
Butterknife; /** *@ class Name: gesturefilpactivity *@ Description: *@ time: 2016/5/3 16:11 *@ Author: lidong *@ mailbox: lidong@chni.com.cn * @company: Chni/Public
 Class Gesturefilpactivity extends Baseactivity implements gesturedetector.ongesturelistener{@Bind (R.id.viewflipper)
 Viewflipper Mviewflipper; 1. DefineGesture Detector Object Gesturedetector mgesturedetector;
 2. Defines an animated array that is used to specify toggle animation effects for Viewfilpper.
 animation[] animations = new Animation[4];

 3. Define the minimum distance between the gesture two points final int flip_distance = 50;
 list<question> mquestion = new arraylist<> ();
 Chinesemedicinereportadapter adapter;
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.LAYOUT.ACTIVITY_GESTURE_FILP);
  Butterknife.bind (this);
  Setactivitytitle ("Realization of the answering device");
  1. Construct gesture Detector mgesturedetector = new Gesturedetector (this,this);
  2 Preparation Data List<question> questions = InitData ();
  Mquestion.addall (questions);
  3. Add child controls for Viewfilpper.
   for (int i = 0;i<mquestion.size (); i++) {Question question = Mquestion.get (i);
  Mviewflipper.addview (Addquestionview (question));
  //4. Initialize animation array Animations[0] = Animationutils.loadanimation (this,r.anim.left_in);
  ANIMATIONS[1] = animationutils.loadanimation (this,r.anim.left_out); ANIMATIONS[2] = ANIMATIONUTILS.LOadanimation (this,r.anim.right_in);

 ANIMATIONS[3] = animationutils.loadanimation (this,r.anim.right_out); @Override protected void Setupactivitycomponent (Appcomponent appcomponent) {} private View Addquestionview (Quest
  Ion question) {View view = View.inflate (this, r.layout.activity_chnihealthreport, null);
  TextView tes = (TextView) View.findviewbyid (r.id.tv_question);
  ListView ListView = (ListView) View.findviewbyid (r.id.lv_question_answer);
  adapter = new Chinesemedicinereportadapter (this,question);
  Listview.setadapter (adapter);
  Tes.settext (Question.getquestion ()); Listview.setonitemclicklistener (New Adapterview.onitemclicklistener () {@Override public void Onitemclick (Adaptervie W<?> Parent, view view, int position, long id) {Toast.maketext (gesturefilpactivity.this,position+ "", Toast.lengt
    H_short). Show ();
    Toast.maketext (gesturefilpactivity.this,position+ "", Toast.length_short). Show (); if (mviewflipper.getdisplayedchild () = = Mquestion.sizE ()-1) {Toast.maketext (Gesturefilpactivity.this, "Last Question", Toast.length_short). Show ();
     Mviewflipper.stopflipping ();
    Return
     }else {mviewflipper.setinanimation (animations[0]);
     Mviewflipper.setoutanimation (Animations[1]);
    Mviewflipper.shownext ();
  }
   }
  });
 return view;
 @Override public boolean Ondown (Motionevent e) {return false; The @Override public void onshowpress (Motionevent e) {} @Override public boolean onsingletapup (Motionevent e) {R
 Eturn false; 
 @Override public boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float Distancey) {return false;  @Override public void onlongpress (Motionevent e) {}//Emphasis implementation Toggles here @Override public boolean onfling (motionevent E1, motionevent E2, float Velocityx, float velocityy) {if (E2.getx ()-E1.getx () >flip_distance) {if (Mviewflippe
    R.getdisplayedchild () = = 0) {mviewflipper.stopflipping (); Toast.maketext (Gesturefilpactivity.this, "the firstTitle ", Toast.length_short). Show ();
   return false;
    else {mviewflipper.setinanimation (animations[2]);
    Mviewflipper.setoutanimation (Animations[3]);
    Mviewflipper.showprevious ();
   return true;
    }else if (E1.getx ()-E2.getx () >flip_distance) {if (mviewflipper.getdisplayedchild () = = Mquestion.size ()-1) {
    Toast.maketext (Gesturefilpactivity.this, "Last Question", Toast.length_short). Show ();
    Mviewflipper.stopflipping ();
   return false;
    }else {mviewflipper.setinanimation (animations[0]);
    Mviewflipper.setoutanimation (Animations[1]);
    Mviewflipper.shownext ();
   return true;
 return false; @Override public boolean ontouchevent (Motionevent event) {//Turn the triggered event on the activity to a gesturedetector handle return this.mge
 Sturedetector.ontouchevent (event);
  Private List<question> InitData () {list<question> questions = new arraylist<> ();
  Question Q1 = new question ();
  Q1.setquestion ("1," "Matchmaker" origin is from which of the following classical plays: "); List<question.answer> MA = new arraylist<> ();
  Question.answer A1 = new Question.answer ();
  A1.setanswermessage ("A pipa");
  Question.answer A2 = new Question.answer ();
  A2.setanswermessage ("The Romance of the West Chamber");
  Question.answer a3 = new Question.answer ();
  A3.setanswermessage ("C" "The Palace of Immortality");
  Question.answer a4 = new Question.answer ();
  A4.setanswermessage ("D" "Peach Blossom Fan");
  Ma.add (A1);
  Ma.add (A2);
  Ma.add (A3);
  Ma.add (A4);
  Q1.setanswer (MA);

  Questions.add (Q1);
  Question Q2 = new question ();
  Q2.setquestion ("2. The first audio film in China is:");
  list<question.answer> MB = new arraylist<> ();
  Question.answer B1 = new Question.answer ();
  B1.setanswermessage ("A Song Needlework Peony");
  Question.answer b2 = new Question.answer ();
  B2.setanswermessage ("B" Dingjun);
  Question.answer B3 = new Question.answer ();
  B3.setanswermessage ("C Lin Zexu");
  Question.answer b4 = new Question.answer ();
  B4.setanswermessage ("Where The Jade Man");
  Mb.add (B1);
  Mb.add (B2);
  Mb.add (B3);
  Mb.add (B4);
  Q2.setanswer (MB);
Questions.add (Q2);  Question q3= new question ();
  Q3.setquestion ("3) the following mountain does not belong to one of China's four major Foshan: (A)");
  list<question.answer> MC = new arraylist<> ();
  Question.answer C1 = new Question.answer ();
  C1.setanswermessage ("A Song Needlework Peony");
  Question.answer C2 = new Question.answer ();
  C2.setanswermessage ("B" Dingjun);
  Question.answer C3 = New Question.answer ();
  C3.setanswermessage ("C Lin Zexu");
  Question.answer C4 = new Question.answer ();
  C4.setanswermessage ("Where The Jade Man");
  Mc.add (C1);
  Mc.add (C2);
  Mc.add (C3);
  Mc.add (C4);
  Q3.setanswer (MC);
  Questions.add (Q3);
  Question Q4 = new question (); Q4.setquestion ("4) which of the following is the correct description of the term" insects "?
  ");
  list<question.answer> MD = new arraylist<> ();
  Question.answer D1 = new Question.answer ();
  D1.setanswermessage ("A Song Needlework Peony");
  Question.answer D2 = new Question.answer ();
  D2.setanswermessage ("B" Dingjun);
  Question.answer d3 = new Question.answer ();
  D3.setanswermessage ("C Lin Zexu");
  Question.answer d4 = new Question.answer (); D4.setanswermessage ("D" Jade Manwhere ");
  Md.add (D1);
  Md.add (D2);
  Md.add (D3);
  Md.add (D4);
  Q4.setanswer (MD);

  Questions.add (Q4);
 return questions;
 }
}

5. Summary

1. Build gesture detectors, 2 prepare data, 3 Add child controls for Viewfilpper. 4. Initialize the animation array. 5. The activity of the TouchEvent incident to Gesturedetector to deal with, 6. Implement Onfling method.

Code download: Android To enable the answer to turn the page effect

This is the entire content of this article, I hope to learn more about Android software programming help.

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.