[AS2.3.3]ViewSwitcher的使用

前言

安卓控件TextSwitcher的使用(实现Textview的上下滚动)
这篇中说到ViewSwitcher的使用,这边算是对Switcher的具体说明


使用类的效果展示

效果展示

使用如下

    List<String> list = new ArrayList<>();

    for (int i = 0; i < 5; i++) {
        list.add("=>>>>"+i);
    }

    vs.setFactory(new ViewSwitcher.ViewFactory() {
        @Override
        public View makeView() {
            return LayoutInflater.from(mContext).inflate(R.layout.item_bar,null,false);
        }
    });

    ViewSwitcherAnimation<String> viewSwitcherAnimation = new ViewSwitcherAnimation<String>(vs,list) {
        @Override
        protected void bindData(String s, View view) {
            ImageView imageView = (ImageView) view.findViewById(R.id.iv_bar);
            TextView textView = (TextView) view.findViewById(R.id.tv_bar);
            imageView.setImageResource(R.mipmap.add);
            textView.setText("count"+s);
        }
    };

基础使用

和TextSwitcher一样也是设置工厂,然后再更新的地方刷新页面就好了

  • xml布局
    <ViewSwitcher
        android:id="@+id/viewSwitcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
  • 代码设置
    ViewSwitcher vs = (ViewSwitcher) findViewById(R.id.viewSwitcher);

    vs.setFactory(new ViewSwitcher.ViewFactory() {
        @Override
        public View makeView() {
            return LayoutInflater.from(mContext).inflate(R.layout.item_bar,null,false);
        }
    });

    int count = 0;

    //前一页
    count--;
    View view = vs.getNextView();
    ImageView imageView = (ImageView) view.findViewById(R.id.iv_bar);
    TextView textView = (TextView) view.findViewById(R.id.tv_bar);
    imageView.setImageResource(R.mipmap.add);
    textView.setText("count___"+count);
    vs.showPrevious();
    //后一页
    count++;
    View view = vs.getNextView();
    ImageView imageView = (ImageView) view.findViewById(R.id.iv_bar);
    TextView textView = (TextView) view.findViewById(R.id.tv_bar);
    imageView.setImageResource(R.mipmap.add);
    textView.setText("count___"+count);
    vs.showNext();

可以看到 只要在showNext()之前调用getNextView()方法就可以获取需要被展示的页面view,然后对其加以设置布局就可以了。


使用类

对此,我也写了一个简单的使用类

ViewSwitcherAnimation.java
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ViewSwitcher;

import java.util.List;

/**
 * ViewSwitcherAnimation
 * Author: gjn.
 * Time: 2018/3/15.
 */

public abstract class ViewSwitcherAnimation<T> {
    private static final String TAG = "ViewSwitcherAnimation";
    private static final int DURATION = 1000;

    private ViewSwitcher viewSwitcher;
    private List<T> datas;
    private int marker;
    private AnimationSet InAnimationSet;
    private AnimationSet OutAnimationSet;

    private int delayTime = 2000;
    private Handler handler = new Handler();
    private Runnable task = new Runnable() {
        @Override
        public void run() {
            nextView();
            handler.postDelayed(task, delayTime * 2);
        }
    };

    public ViewSwitcherAnimation(ViewSwitcher viewSwitcher, List<T> views) {
        this.viewSwitcher = viewSwitcher;
        this.datas = views;
    }

    public void start() {
        stop();
        handler.postDelayed(task, delayTime);
    }

    public void stop(){
        handler.removeCallbacks(task);
    }

    public ViewSwitcherAnimation setData(List<T> views) {
        this.datas = views;
        return this;
    }

    public int getMarker() {
        return marker;
    }

    public void setDelayTime(int delayTime) {
        this.delayTime = delayTime;
    }

    public void create(){
        marker = -1;
        if (datas == null){
            Log.w(TAG, "datas is null");
            return;
        }
        if (viewSwitcher == null) {
            Log.w(TAG, "viewSwitcher is null");
            return;
        }
        nextView();
        createAnimation();
        viewSwitcher.setInAnimation(InAnimationSet);
        viewSwitcher.setOutAnimation(OutAnimationSet);
        start();
    }


    private void createAnimation() {
        AlphaAnimation alphaAnimation;
        TranslateAnimation translateAnimation;

        int h = viewSwitcher.getHeight();
        if (h <= 0) {
            viewSwitcher.measure(0,0);
            h = viewSwitcher.getMeasuredHeight();
        }

        InAnimationSet = new AnimationSet(true);
        OutAnimationSet = new AnimationSet(true);

        alphaAnimation = new AlphaAnimation(0,1);
        translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
                Animation.ABSOLUTE, h, Animation.ABSOLUTE, 0);
        InAnimationSet.addAnimation(alphaAnimation);
        InAnimationSet.addAnimation(translateAnimation);
        InAnimationSet.setDuration(DURATION);

        alphaAnimation = new AlphaAnimation(1,0);
        translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
                Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -h);
        OutAnimationSet.addAnimation(alphaAnimation);
        OutAnimationSet.addAnimation(translateAnimation);
        OutAnimationSet.setDuration(DURATION);
    }

    private void nextView() {
        marker = ++marker % datas.size();
        View view = viewSwitcher.getNextView();
        bindData(datas.get(marker),view);
        viewSwitcher.showNext();
    }

    protected abstract void bindData(T t, View view);
}
ViewFlipperViewSwitcher使用:屏幕切换指的是在同一个Activity内屏幕见的切换,最长见的情况就是在一个FrameLayout内有多个页面,比如一个系统设置页面;一个个性化设置页面。 通过查看OPhone API文档可以发现,有个android.widget.ViewAnimator类继承至FrameLayout,ViewAnimator类的作用是为FrameLayout里面的View切换提供动画效果。该类有如下几个和动画相关的函数: l setInAnimation:设置View进入屏幕时候使用的动画,该函数有两个版本,一个接受单个参数,类型为android.view.animation.Animation;一个接受两个参数,类型为Context和int,分别为Context对象和定义Animation的resourceID。 setOutAnimation: 设置View退出屏幕时候使用的动画,参数setInAnimation函数一样。 showNext: 调用该函数来显示FrameLayout里面的下一个View。 showPrevious:调用该函数来显示FrameLayout里面的上一个View。 一般不直接使用ViewAnimator而是使用它的两个子类ViewFlipperViewSwitcherViewFlipper可以用来指定FrameLayout内多个View之间的切换效果,可以一次指定也可以每次切换的时候都指定单独的效果。该类额外提供了如下几个函数: isFlipping: 用来判断View切换是否正在进行 setFilpInterval:设置View之间切换的时间间隔 startFlipping:使用上面设置的时间间隔来开始切换所有的View,切换会循环进行 stopFlipping: 停止View切换 ViewSwitcher 顾名思义Switcher特指在两个View之间切换。可以通过该类指定一个ViewSwitcher.ViewFactory 工程类来创建这两个View。该类也具有两个子类ImageSwitcher、TextSwitcher分别用于图片和文本切换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值