Android uses viewflipper for page switching and uses gesture sliding Switching

Source: Internet
Author: User

The Android system comes with a multi-Page Management Control: viewflipper.

It allows you to easily switch between subpages ,,,

It only needs to add several views using the addview method. Each view corresponds to a page, which can be used to manage multiple pages ,,,

It is also relatively simple to implement Gesture Recognition on Android, And the ontouchlistener and onguesturelistener interfaces can be implemented,

Then register gesturedetector in the ontouch function to identify gesture actions,

Refer to an article by Daniel:

Http://wang-peng1.iteye.com/blog/572886

Gesturedetector. ongesturelistener: Used to notify normal gesture events. This interface has the following six callback functions:
1. ondown (motionevent E): Down event;
2. onsingletapup (motionevent E): one-click up event;
3. onshowpress (motionevent E): When the down event occurs and the move or up event is not triggered before the event is executed;
4. onlongpress (motionevent E): Long press event;
5. onfling (motionevent E1, motionevent E2, float velocityx, float velocityy): Sliding gesture event;
6. onscroll (motionevent E1, motionevent E2, float distancex, float distancey): drag the event on the screen.

The main judgment is that in the onfling () function, E1 indicates the position information starting to press down, and E2 indicates the position information when it is lifted, therefore, you can determine whether to slide left or right through the Distance Difference between them on the X axis...

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,          float velocityY) {      // TODO Auto-generated method stub       if (e2.getX()-e1.getX() > 100) {          // fling right           showNextView();      } else if (e1.getX() - e2.getX() > 100) {          // fling left           showPreviousView();      }      return false;  }  

Resource 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">  <LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="wrap_content">      <Button        android:id="@+id/btnPrev"        android:text="Previous"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />      <Button        android:id="@+id/btnNext"        android:text="Next"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />  </LinearLayout>  <ViewFlipper     android:id="@+id/vfFlingTest"     android:layout_width="fill_parent"     android:layout_height="fill_parent"></ViewFlipper>  </LinearLayout> 

Code:

import android.app.Activity;  import android.os.Bundle;  import android.view.GestureDetector;  import android.view.KeyEvent;  import android.view.MotionEvent;  import android.view.View;  import android.view.GestureDetector.OnGestureListener;  import android.view.View.OnClickListener;  import android.view.View.OnTouchListener;  import android.view.ViewGroup.LayoutParams;  import android.view.animation.AnimationUtils;  import android.widget.Button;  import android.widget.EditText;  import android.widget.ImageView;  import android.widget.TextView;  import android.widget.ViewFlipper;  public class FlingTest extends Activity implements              OnTouchListener, OnGestureListener{      private Button btnPrev;      private Button btnNext;      private ViewFlipper vfFlingTest;            private TextView tvFlipper;      private EditText etFlipper;      private ImageView ivFlipper;            private GestureDetector mGestureDetector;            @Override      protected void onCreate(Bundle savedInstanceState) {          // TODO Auto-generated method stub           super.onCreate(savedInstanceState);          setContentView(R.layout.fling_test);                    btnPrev = (Button)findViewById(R.id.btnPrev);          btnNext = (Button)findViewById(R.id.btnNext);          vfFlingTest = (ViewFlipper)findViewById(R.id.vfFlingTest);                    initViews();                    vfFlingTest.addView(tvFlipper);          vfFlingTest.addView(etFlipper);          vfFlingTest.addView(ivFlipper);                    vfFlingTest.setOnTouchListener(this);          vfFlingTest.setLongClickable(true);          mGestureDetector = new GestureDetector(this);                    btnPrev.setOnClickListener(new OnClickListener() {                            public void onClick(View v) {                  // TODO Auto-generated method stub                   showPreviousView();              }          });                    btnNext.setOnClickListener(new OnClickListener() {                            public void onClick(View v) {                  // TODO Auto-generated method stub                   showNextView();              }          });      }            public void showPreviousView() {          vfFlingTest.setInAnimation(AnimationUtils.loadAnimation(                  this, R.anim.right_in));          vfFlingTest.setOutAnimation(AnimationUtils.loadAnimation(                  this, R.anim.left_out));          vfFlingTest.showPrevious();      }            public void showNextView() {          vfFlingTest.setInAnimation(AnimationUtils.loadAnimation(                  this, R.anim.left_in));          vfFlingTest.setOutAnimation(AnimationUtils.loadAnimation(                  this, R.anim.right_out));          vfFlingTest.showNext();      }            private void initViews() {          tvFlipper = new TextView(this);          tvFlipper.setText("this is a text view!");                    etFlipper = new EditText(this);          etFlipper.setText("this is a text view!");                    ivFlipper = new ImageView(this);          ivFlipper.setLayoutParams(new LayoutParams(                  LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));          ivFlipper.setImageResource(R.drawable.pic1);      }      @Override      protected void onDestroy() {          // TODO Auto-generated method stub           android.os.Process.killProcess(android.os.Process.myPid());          super.onDestroy();      }      @Override      public boolean onKeyDown(int keyCode, KeyEvent event) {          // TODO Auto-generated method stub           if (keyCode == KeyEvent.KEYCODE_BACK) {              finish();              return true;          }          return super.onKeyDown(keyCode, event);      }      public boolean onTouch(View view, MotionEvent event) {          // TODO Auto-generated method stub           return mGestureDetector.onTouchEvent(event);      }        public boolean onDown(MotionEvent arg0) {          // TODO Auto-generated method stub           return false;      }      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,              float velocityY) {          // TODO Auto-generated method stub           if (e2.getX()-e1.getX() > 100) {              // fling right               showNextView();          } else if (e1.getX() - e2.getX() > 100) {              // fling left               showPreviousView();          }          return false;      }      public void onLongPress(MotionEvent e) {          // TODO Auto-generated method stub                 }      public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,              float distanceY) {          // TODO Auto-generated method stub           return false;      }      public void onShowPress(MotionEvent e) {          // TODO Auto-generated method stub                 }      public boolean onSingleTapUp(MotionEvent e) {          // TODO Auto-generated method stub           return false;      }  }  

There is also a simple page switching animation effect (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="500" />   </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="500" />   </set> 

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="500" />   </se

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

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.