Can't add more than 2 views to a ViewSwitcher

最近使用到  TextSwitcher 来实现文字上下轮播的效果,出现了

Can't add more than 2 views to a ViewSwitcher

看了下源码

    /**
     * {@inheritDoc}
     *
     * @throws IllegalStateException if this switcher already contains two children
     */
    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        if (getChildCount() >= 2) {
            throw new IllegalStateException("Can't add more than 2 views to a ViewSwitcher");
        }
        super.addView(child, index, params);
    }

我的出现问题的原因是第一次没有问题显示,再次返回这个界面的时候提示的,也就是有addView 一次

于是我写了一个判断当getChildCount<2 的时候 在list  调用 setFactory 方法,这样就好了。

自己判断的代码

发布了675 篇原创文章 · 获赞 279 · 访问量 71万+

猜你喜欢

转载自blog.csdn.net/qq_33210042/article/details/105178168