Android ViewFlipper Usage Example Analysis

  • 2021-01-22 05:22:06
  • OfStack

This article illustrates Android ViewFlipper usage with examples. To share with you for your reference, as follows:

The effect here is to display images one by one when manually swiping the phone screen, one at a time


package com.my.viewflippertest;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class ViewFlipperTestActivity extends Activity implements OnGestureListener {
 private ViewFlipper vf;// The statement 1 a ViewFlipper object 
 private GestureDetector gd;
 private int[] d={// define 1 A collection of images 
  R.drawable.a,
  R.drawable.b,
  R.drawable.c,
  R.drawable.d,
  R.drawable.e,
  R.drawable.f,
  R.drawable.h,
  R.drawable.i,
  R.drawable.g
 };
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    gd=new GestureDetector(this);
    vf=(ViewFlipper)this.findViewById(R.id.vf);// Initialize object 
    for(int i=0;i<d.length;i++){
     vf.addView(addTextView(d[i]));// Add the interface 
    }
  }
  public TextView addTextView(int a){// Put pictures in TextView In the 
   TextView tv=new TextView(this);
   tv.setBackgroundResource(a);
   return tv;
  }
  // Trigger contact event 
  public boolean onTouchEvent(MotionEvent event){
   return this.gd.onTouchEvent(event);
  }
  // Display after the contact event has occurred 1 image 
  public boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY){
   this.vf.showNext();
   return true;
  }
 @Override
 public boolean onDown(MotionEvent e) {
 // TODO Auto-generated method stub
 return false;
 }
 @Override
 public void onLongPress(MotionEvent e) {
 // TODO Auto-generated method stub
 }
 @Override
 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
  float distanceY) {
 // TODO Auto-generated method stub
 return false;
 }
 @Override
 public void onShowPress(MotionEvent e) {
 // TODO Auto-generated method stub
 }
 @Override
 public boolean onSingleTapUp(MotionEvent e) {
 // TODO Auto-generated method stub
 return false;
 }
}

For more information on Android development, please check out the Android Introduction and Advanced Course.

I hope this article is helpful to Android program design.


Related articles: