Android 公告轮播组件

项目需要一个首页轮播组件,所以自己简单写了一个。
主要是项目页面每次在回来时要刷新数据,所以导致整个轮播
又要重新刷新一次,体验非常不好,所以在这儿进行了部分优
化。使其在每次设置新数据时,更加perfect。嘿嘿
直接上代码:

public class VerticalRollTextView extends TextSwitcher implements ViewSwitcher.ViewFactory {

    private static final int FLAG_START_AUTO_SCROLL = 199;

    private float mTextSize = 16 ;
    private int stillTime = 3000;
    private int textColor = Color.BLACK;
    private int lines = -1;
    private boolean isRunning = false;

    private OnItemClickListener itemClickListener;
    private Context mContext;
    private int currentId = -1;
    private ArrayList<String> textList = new ArrayList<String>();

    public VerticalRollTextView(Context context) {
        this(context, null);
        mContext = context;
    }

    public VerticalRollTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        TypedArray a = context.obtainStyledAttributes(attrs,      R.styleable.VerticalRollTextView);
        textColor = a.getColor(R.styleable.VerticalRollTextView_rollTextColor, Color.BLACK);
        mTextSize = a.getDimension(R.styleable.VerticalRollTextView_rollTextSize, 16);
        lines = a.getInteger(R.styleable.VerticalRollTextView_rollLines,-1);
        textList = new ArrayList<String>();
    }

    public void setAnimTime(long animDuration) {
        if (getChildCount() != 2) {
            removeAllViews();
        }
        setFactory(this);
        Animation in = new TranslateAnimation(0, 0, 100, 0);
        in.setDuration(animDuration);
        in.setInterpolator(new AccelerateInterpolator());
        Animation out = new TranslateAnimation(0, 0, 0, -100);
        out.setDuration(animDuration);
        out.setInterpolator(new AccelerateInterpolator());
        setInAnimation(in);
        setOutAnimation(out);
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            while (isRunning){
                handler.sendEmptyMessage(FLAG_START_AUTO_SCROLL);
                SystemClock.sleep(stillTime);
            }
        }
    };

    private Handler handler =new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == FLAG_START_AUTO_SCROLL&&textList != null) {
                    if (textList.size() > 0) {
                        currentId++;
                        setText(textList.get(currentId % textList.size()));
                    }
                }
            }
        };
    /**
     * 设置数据源
     * @param titles
     */
    public void setTextList(ArrayList<String> titles) {
        if (titles == null){
            isRunning = false;
            return;
        }
        if (textList.toString().equals(titles.toString())){
            return;
        }
        textList = titles;
        if (titles.size() == 1){
            currentId = 0;
            setText(textList.get(currentId % textList.size()));
            isRunning = false;
            return;
        }
        startAutoScroll();
    }

    /**
     * 开始滚动
     */
    public void startAutoScroll() {
        if (isRunning == false){
            isRunning = true;
            Thread thread = new Thread(runnable);
            currentId = -1;
            thread.start();
        }
    }

    /**
     * 停止滚动
     */
    public void stopAutoScroll() {
        isRunning = false;
    }

    @Override
    public View makeView() {
        TextView t = new TextView(mContext);
        t.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        t.setLayoutParams(layoutParams);
        if (lines != -1) {
            t.setLines(lines);
        }
        t.setTextColor(textColor);
        t.setTextSize(TypedValue.COMPLEX_UNIT_PX,mTextSize);
        t.setEllipsize(TextUtils.TruncateAt.END);
        t.setClickable(true);
        t.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener != null && textList.size() > 0 && currentId != -1) {
                    itemClickListener.onItemClick(currentId % textList.size());
                }
            }
        });
        return t;
    }

    /**
     * @param textSize 字号
     * @param textColor 字体颜色
     */
    public void setText(float textSize,int textColor) {
        mTextSize = textSize;
        this.textColor = textColor;
    }

    public void setStillTime(int stillTime){
        this.stillTime = stillTime;
    }

    /**
     * 设置点击事件监听
     * @param itemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    /**
     * 轮播文本点击监听器
     */
    public interface OnItemClickListener {
        /**
         * 点击回调
         * @param position 当前点击ID
         */
        void onItemClick(int position);
    }

}
<!--公告轮播器-->
    <declare-styleable name="VerticalRollTextView">
        <attr name="rollTextColor" format="color"/>
        <attr name="rollTextSize" format="dimension"/>
        <attr name="rollPadding" format="dimension"/>
        <attr name="rollLines" format="integer"/>
    </declare-styleable>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值