公告栏目纵向滚动

ViewFlipper是这安卓自带一个主键来实现这个效果,下面是实现方式
public class UPMarqueeView extends ViewFlipper {
    private Context mContext;
    private boolean isSetAnimDuration = false;
    private int interval = 3000;
    /**
     * 动画时间
     */
    private int animDuration = 1000;

    public UPMarqueeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        this.mContext = context;
        setFlipInterval(interval);
        Animation animIn = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_in);
        if (isSetAnimDuration) {
            animIn.setDuration(animDuration);
        }
        setInAnimation(animIn);
        Animation animOut = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_out);
        if (isSetAnimDuration) {
            animOut.setDuration(animDuration);
        }
        setOutAnimation(animOut);
    }


    /**
     * 设置循环滚动的View数组
     *
     * @param views
     */
    public void setViews(final List views) {
        if (views == null || views.size() == 0) return;
        removeAllViews();
        for ( int i = 0; i < views.size(); i++) {
            final int position=i;
            //设置监听回调
            views.get(i).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(position, views.get(position));
                    }
                }
            });
            ViewGroup viewGroup = (ViewGroup) views.get(i).getParent();
            if (viewGroup != null) {
                viewGroup.removeAllViews();
            }
            addView(views.get(i));
        }
        startFlipping();
    }

    /**
     * 点击
     */
    private OnItemClickListener onItemClickListener;

    /**
     * 设置监听接口
     * @param onItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * item_view的接口
     */
    public interface OnItemClickListener {
        void onItemClick(int position, View view);
    }
}
TextSwitcher第二安卓新出的一个也不算太新吧,但是你在网上查首先大家肯定让你用第一种,但是第一
种有个致命问题,重影问题, 好多库说跟着生命周期结束就可以,但是不行,都是你抄我的我抄你的,
你们都不试一试就拿出说呀,真是牛,第二这个完美的解决了这个问题,亲测,
第三中就是自己写,但是大家要明白,自定义是在你没有谷歌控件,或者无法满足的时候才写,有控件
可以实现为何不用,性能,刷新都可以,所以我推荐第二种,到时候我会把demo放上,把之前侧回替换,
至于代码很简单。
/**
 * 解决重影问题滚动问题
 */
private void setTextSwitcher() {
    mTextSwitcher.setInAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.slide_in_bottom));
    mTextSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.slide_out_top));
    mTextSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
        @Override
        public View makeView() {
        //    View moreView =LayoutInflater.from(getContext()).inflate(R.layout.item_view, null);
            TextView textView = new TextView(getContext());
            textView.setSingleLine();
            textView.setTextSize(14);//字号
            textView.setTextColor(Color.parseColor("#666666"));
            textView.setEllipsize(TextUtils.TruncateAt.END);
            textView.setSingleLine();
            textView.setGravity(Gravity.CENTER_VERTICAL);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            params.gravity = Gravity.CENTER;
            textView.setLayoutParams(params);
            textView.setPadding(5, 0, 25, 0);
            textView.setOnClickListener((v)->{

                MobclickAgent.onEvent(getContext(), "rl");
                if (!isNetWorks()) {
                    ToastUtils.show("您的网络不给力,请稍后再试");
                } else {
                    Logger.d("比较俩个值:"+index);
                    Logger.d("集合长度:"+dataList.size());
                     {
                        Intent mIntent = new Intent(getActivity(), APPWebviewActivity.class);
                        mIntent.putExtra(APPWebviewActivity.URL, dataList.get(index).href);
                        getActivity().startActivity(mIntent);
                    }

                }


            });

            return textView;
        }
    });
}

private Runnable runnable = new Runnable() {
    @Override
    public void run() {
        if (!isFlipping) {
            return;
        }
        index++;
        mTextSwitcher.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() {
    if (mWarningTextList.size() == 1) {
        mTextSwitcher.setText(mWarningTextList.get(0));
        index = 0;
    }
    if (mWarningTextList.size() > 1) {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mTextSwitcher.setText(mWarningTextList.get(0));
                index = 0;
            }
        }, 1000);
        mTextSwitcher.setInAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.slide_in_bottom));
        mTextSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.slide_out_top));

        startFlipping();
    }
}

你可能感兴趣的:(基础知识)