Android ViewFlipper 使用

使用ViewFlipper 实现条目向上滚动功能

1、布局

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">

            <ViewFlipper
                android:id="@+id/flipper"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:flipInterval="150"
                android:inAnimation="@anim/notice_in"
                android:outAnimation="@anim/notice_out">

                <TextView
                    android:id="@+id/tvOriginal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="0.01" />
            </ViewFlipper>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="元" />
        </LinearLayout>

2、新建动画文件 notice_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:duration="100"
        android:fromYDelta="0.0"
        android:toYDelta="-100.0%p"/>

    <alpha
        android:duration="100"
        android:fromAlpha="1.0"
        android:toAlpha="0.0"/>

</set>

 3、新建动画文件 notice_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:duration="100"
        android:fromYDelta="100.0%p"
        android:toYDelta="0.0"/>

    <alpha
        android:duration="100"
        android:fromAlpha="0.0"
        android:toAlpha="1.0"/>

</set>

4、代码控制

    public void startFlipping() {
        final ViewFlipper flipper = findViewById(R.id.flipper);
        List<String> infos = Arrays.asList("0.01", "0.02", "0.03", "0.04", "0.05");
        for (int i = 0; i < infos.size(); i++) {
            TextView textView = new TextView(CustomerViewActivity.this);
            textView.setText(infos.get(i));
            flipper.addView(textView);
        }

        //开始启动轮播
        flipper.startFlipping();
        
        
        //监听禁止循环轮播
        flipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {

            public void onAnimationStart(Animation animation) {
            }

            public void onAnimationRepeat(Animation animation) {
            }

            public void onAnimationEnd(Animation animation) {

                int displayedChild = flipper.getDisplayedChild();
                int childCount = flipper.getChildCount();

                if (displayedChild == childCount - 1) {
                    flipper.stopFlipping();
                }
            }
        });
    }

简简单单不好吗,非得搞得多复杂!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值