广告自动滚动

最近做项目,遇到类似广告自动上下滚动的效果,每次滚动一行,一行就是一个标题。

开始呢,使用到listview里面的一个方法smoothScrollToPosition会有bug,然后公司一个同事写的一个自定义的scrollview来写,效果不错,拿来用用。实现起来很简单的


import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/27.
 */
public class NewScrollView extends ScrollView {
    private static final String TAG = "NewsScrollView";
    private static int HEIGHT = 0; /* 控件高度 */
    private static final int TIME_OFFSET = 2000; /* 滚动速度 */
    private static Context context;

    private List<String> datas = new ArrayList<String>(); /* 数据源(需要改成实体) */

    private int scroll_index; // 当前新闻滚动到的条数

    public NewsCallback callback;

    public static int dipToPixels(float dip) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f);
    }

    public NewScrollView(Context context) {
        super(context);
        this.context = context;
        HEIGHT = dipToPixels(20);
    }

    public NewScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        HEIGHT = dipToPixels(20);
    }

    /* 初始化 */
    public void initialize(List<String> datas) {
        this.datas = datas;
        this.setLayoutParams(new ScrollView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, HEIGHT));
        this.datas.add(datas.get(0));
        this.setVerticalScrollBarEnabled(false);
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        scroll_index = 0;

        addChilds();
    }

    public void setCallback(NewsCallback callback) {
        this.callback = callback;
    }

    private void addChilds() {
        LinearLayout container = new LinearLayout(context);
        container.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, HEIGHT * datas.size()));
        container.setOrientation(LinearLayout.VERTICAL);
        this.addView(container);

        // 创建textview,将其放在linearlayout里面
        for (int i = 0; i < datas.size(); i++) {
            TextView tvChild = new TextView(context);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, HEIGHT);
            layoutParams.setMargins(20, 0, 20, 0);
            // 设置每行要显示textview的一些属性
            tvChild.setLayoutParams(layoutParams);
            tvChild.setText(datas.get(i));
            tvChild.setTextColor(Color.BLACK);
            tvChild.setSingleLine(true);
            tvChild.setEllipsize(TextUtils.TruncateAt.END);
            tvChild.setTextSize(14);
            container.addView(tvChild);
            tvChild.setTag(i);
            tvChild.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
        }

        this.post(new Runnable() {
            @Override
            public void run() {
                timer.removeMessages(0);
                timer.sendEmptyMessageDelayed(0, TIME_OFFSET);
            }
        });
    }


    /* 滚动逻辑 */
    private Handler timer = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if (scroll_index == datas.size() - 1) {
                scroll_index = 0;
                /* 滚动到了最后一页 */
                scrollTo(0, 0);
                scroll_index++;
            } else {
                scroll_index++;
            }
            // 滚动scrollview
            smoothScrollTo(0, HEIGHT * scroll_index);
            timer.sendEmptyMessageDelayed(0, TIME_OFFSET);
        }
    };


    public interface NewsCallback {
        
        void onClickNews(int position);
    }

}




这是自定义的scrollview,用的时候调用initialize(List<String>datas),放入string的集合,就可以了,当然有具体特定需求改改自定义的scrollview就可以了。

具体实现就是,再要放入广告滚动的地方,加入自定义scrollview,传入list集合,传入进集合后,会在这个集合后面多加入一条数据(第一条数据)


this.datas.add(datas.get(0));
为了就是滑动到最后一条数据的时候,又滚动到了第一页,Handler里面有看到。addChilds()方法中,先为scrollview加入一个线性布局高度为集合高度*每一行的高度,然后为这个布局添加textview
动态gif图不知道怎么弄,所有就没有图了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值