跑马灯实现

项目中要实现跑马灯垂直滚动功能,选择的TextSwitcher

布局xml:

<TextSwitcher
    android:id="@+id/text_switcher"
    style="@style/textStyle_remark"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/dp_3"
    android:layout_gravity="center_vertical" />

控件初始化及处理

private void setTextSwitcher() {
    textSwitcher.setInAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.anim_marquee_in));
    textSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.anim_marquee_out));
    textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
        @Override
        public View makeView() {
            textView = new TextView(getContext());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                textView.setTextAppearance(R.style.textStyle_remark_gray);
            }

            textView.setEllipsize(TextUtils.TruncateAt.MIDDLE);
            textView.setSingleLine();
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            params.gravity = Gravity.CENTER_VERTICAL;
            textView.setLayoutParams(params);
            textView.setPadding(0, 0, 25, 0);
            return textView;
        }
    });
}

private Runnable runnable = new Runnable() {
    @Override
    public void run() {
        if (!isFlipping) {
            return;
        }
        index++;
        textSwitcher.setText(mWarningTextList.get(index % mWarningTextList.size()));
        if (index == mWarningTextList.size()) {
            index = 0;
        }
        startFlipping();
    }
};

//开启信息轮播
public void startFlipping() {
    if (mWarningTextList.size() > 1) {
        handler.removeCallbacks(runnable);
        isFlipping = true;
        handler.postDelayed(runnable, 3000);
    }
}

//关闭信息轮播
public void stopFlipping() {
    if (mWarningTextList.size() > 1) {
        isFlipping = false;
        handler.removeCallbacks(runnable);
    }
}

//设置数据
private void setData(List<MessageBean> datas) {
    if (datas == null){// 查询失败处理
        llNew.setVisibility(View.GONE);
        return;
    }
    // 最多显示消息列表前3项
    for (int i = 0; i< (datas.size() > 3? 3 : datas.size()); i++){
        getSwitchText(datas, i);
    }

    if (mWarningTextList.size() == 0){
        llNew.setVisibility(View.GONE);
    }
    if (mWarningTextList.size() == 1) {
        textSwitcher.setText(mWarningTextList.get(0));
        index = 0;
    }
    if (mWarningTextList.size() > 1) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                textSwitcher.setText(mWarningTextList.get(0));
                index = 0;
            }
        }, 1000);
        textSwitcher.setInAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.anim_marquee_in));
        textSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.anim_marquee_out));
        startFlipping();
    }
}

private void getSwitchText(List<MessageBean> mes, int position){
    int type = mes.get(position).businessType;//1计划 2培训班 3问卷
    if (type==2){
        mWarningTextList.add(mes.get(position).businessTitle + "即将开课啦!");
    }else if (type==3){
        mWarningTextList.add("快来参与\"" + mes.get(position).businessTitle + "\"问卷调查吧~");
    }else {
        mWarningTextList.add("您的\"" + mes.get(position).businessTitle + "\"计划即将要开始了!");
    }
}

其他可以实现跑马灯功能的方式还有:
A 使用系统控件ViewFlipper;
B 用第三方框架: ‘com.sunfusheng:marqueeview:1.3.3’

参考链接:跑马灯功能的实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值