Android ViewFlipper简单用法解析

ViewFlipper和ViewPager挺像的,都是一个view容器。内部可以添加多个view,只是viewpager可以通过左右滑动来切换view,而viewFlipper则没有这个功能,所以需要在它上面监听手势。比较方便的是它不用使用适配器就能添加view,所以比较方便。

 

首先在布局文件中定义这个控件: 

<?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.在代码中找到并且添加view 

  @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"));//添加view到flipper中,这里添加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 一个textView对象
   */
  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;
  }

3.通过touch监听手势来切换view 

@Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
      float velocityY) {
    if (e1.getX() - e2.getX() > 120) {
      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();//切换到下一个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;
  }

下面是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 when the activity is first created. */
  @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"));//添加view到flipper中,这里添加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 一个textView对象
   */
  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() > 120) {
      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();//切换到下一个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;
  }

  @Override
  public void onLongPress(MotionEvent e) {
    // TODO 自动生成的方法存根
    
  }

  @Override
  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
      float distanceY) {
    // TODO 自动生成的方法存根
    return false;
  }

  @Override
  public void onShowPress(MotionEvent e) {
    // TODO 自动生成的方法存根
    
  }

  @Override
  public boolean onSingleTapUp(MotionEvent e) {
    // TODO 自动生成的方法存根
    return false;
  }


}

动画文件: 

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="500" />
  <alpha android:fromAlpha="0.1" android:toAlpha="1.0"
    android:duration="500" />
</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="500" />
</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="500" />
  <alpha android:fromAlpha="0.1" android:toAlpha="1.0"
    android:duration="500" />
</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="500" />
  <alpha android:fromAlpha="1.0" android:toAlpha="0.1"
    android:duration="500" />
</set>

源码下载:http://xiazai.jb51.net/201609/yuanma/AndroidViewFlip(jb51.net).rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。