【Android 控件】垂直滚动广告栏控件实现(跑马灯效果)

跑马灯TextView是每个学过android开发的人在入门阶段都会去自己实现一遍的。然而,在实际应用当中,要在跑马灯中展示的不仅仅是一行文字,常常是一幅幅图片,甚至是图文混排的广告。

这种场景下,集成了跑马灯效果的TextView肯定就不能满足我们的需求了。经过一番调研,我找到了一种能非常好实现垂直滚动广告栏的控件——ViewFlipper

先看下实现效果。

具体实现起来,代码也是非常清爽。

1、xml布局

(1)ViewFlipper布局

 

<ViewFlipper
                            android:id="@+id/nearby_flipper"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginLeft="6dp"
                            android:layout_weight="1"
                            android:autoStart="true"
                            android:flipInterval="2000"
                            android:inAnimation="@anim/anim_come_in"
                            android:outAnimation="@anim/anim_come_out" />

(2)子View布局

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ellipsize="end"
            android:singleLine="true"
            android:textColor="#222222"
            android:textSize="16dp" />

        <TextView
            android:id="@+id/price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:textColor="#cccccc"
            android:textSize="16dp" />
    </LinearLayout>

    <TextView
        android:id="@+id/distance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="5dp"
        android:gravity="center_vertical"
        android:textColor="#cccccc"
        android:textSize="16dp" />
</LinearLayout>

2、android代码

 

实现起来非常简单,其实就是add View到viewFlipper中。

for (int i = 0; i < list.size(); i++) {
            View view = LayoutInflater.from(context).inflate(R.layout.cttour_around_scroll_tv, null);
            ((TextView) view.findViewById(R.id.name)).setText(list.get(i).getName());
            String str = "¥" + list.get(i).getPrice() + "起";
            SpannableString s = new SpannableString(str);
            s.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.cttour_blue_independent_travel_yellow)), str.indexOf("¥"), str.indexOf("起"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            ((TextView) view.findViewById(R.id.price)).setText(s);
            ((TextView) view.findViewById(R.id.distance)).setText(revisedDistance(list.get(i).getDistance()));
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String url = "/ticket/index.html#/webapp/ticket/dest/n-附近-0/s-tickets?radiusmax=10&sort=6&target=ticket" + "&lon=" + cityModel.getLongitude() + "&lat=" + cityModel.getLatitude();
                    CtripH5Manager.openUrl(context, url, null);
                }
            });
            viewFlipper.addView(view);
        }


如需要demo,或者有任何问题,可以邮件联系我zhshan@ctrip.com

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值