最近使用到 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 方法,这样就好了。
自己判断的代码