Ultra-Pull-To-Refresh动态添加View不能正常显示BUG解决方法
来源:互联网 发布:ios更新系统 数据丢失 编辑:程序博客网 时间:2024/04/27 08:38
上文讲到Ultra-Pull-To-Refresh框架的简单示例,在动态添加PtrFrameLayout或者PtrClassicFrameLayout时踩到坑了,无法显示添加的子View。代码如下:
PtrFrameLayout ptrLayout = new PtrFrameLayout(this);
ptrLayout.setLayoutParams(new PtrFrameLayout.LayoutParams (PtrFrameLayout.LayoutParams.MATCH_PARENT,PtrFrameLayout.LayoutParams.MATCH_PARENT));
WebVeiw mWeb = new WebView(this);
mWeb.setLayoutParams(new ViewGroup.LayoutParams (ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
ptrLayout.addView(mWeb);
结果就是提示如下信息
The content view in PtrFrameLayout is empty. Do you forget to specify its id in xml layout file?
通过查看源码,PtrFrameLayout这个ViewGroup动态添加子View时,还未加进来就调用了onFinishInflate方法,此时获取的子View对象当然为空;所以通过手动调用onFinishInflate()方法来再次获取子View。
对PtrFrameLayout源码加入以下一段代码:
public void addViewForPtrFrameLayout(View child) {
super.addView(child);
if (this.getContentView() == null) {//获取不到子View时再调用一次onFinishInfalte()
this.onFinishInflate();
}
}
然后将原来的ptrLayout.addView(mWeb)改用addViewForPtrFrameLayout(mWeb),来动态添加WebView,这样修改并未让WebView正常显示出来,别急还差一步。看下onFinishInflate()源码
protected void onFinishInflate() {
final int childCount = getChildCount();
if (childCount > 2) {
throw new IllegalStateException("PtrFrameLayout only can host 2 elements");
} else if (childCount == 2) {
if (mHeaderId != 0 && mHeaderView == null) {
mHeaderView = findViewById(mHeaderId);
}
if (mContainerId != 0 && mContent == null) {
mContent = findViewById(mContainerId);
}
// not specify header or content
if (mContent == null || mHeaderView == null) {
View child1 = getChildAt(0);
View child2 = getChildAt(1);
if (child1 instanceof WDPtrUIHandler) {
mHeaderView = child1;
mContent = child2;
} else if (child2 instanceof WDPtrUIHandler) {
mHeaderView = child2;
mContent = child1;
} else {
// both are not specified
if (mContent == null && mHeaderView == null) {
mHeaderView = child1;
mContent = child2;
}
// only one is specified
else {
if (mHeaderView == null) {
mHeaderView = mContent == child1 ? child2 : child1;
} else {
mContent = mHeaderView == child1 ? child2 : child1;
}
}
}
}
} else if (childCount == 1) {
mContent = getChildAt(0);
} else {
mIsFinishInflate = true;
TextView errorView = new TextView(getContext());
errorView.setClickable(true);
errorView.setTextColor(0xffff6600);
errorView.setGravity(Gravity.CENTER);
errorView.setTextSize(20);
errorView.setText("The content view in PtrFrameLayout is empty. Do you forget to specify its id in xml layout file?");
mContent = errorView;
addView(mContent);
}
if (mHeaderView != null) {
mHeaderView.bringToFront();
}
super.onFinishInflate();
}
第一次加载失败时,添加了一个errorView的TextView;再次调用onFinishInflate()方法时,此时ViewGroup存在三个子View:TextView、Header、WebView。childCount() > 3,然后就抛异常了。将45-53行部分代码删除或者改为其他非添加子View的处理就OK了。
0 0
- Ultra-Pull-To-Refresh动态添加View不能正常显示BUG解决方法
- android-Ultra-Pull-To-Refresh
- Android-Ultra-Pull-To-Refresh
- android-Ultra-Pull-To-Refresh-下拉自定义显示动画1
- android-Ultra-Pull-to-Refresh下拉刷新
- android-Ultra-Pull-To-Refresh 源码解析
- android-Ultra-Pull-To-Refresh 源码解析
- android-Ultra-Pull-To-Refresh源码分析
- android-Ultra-Pull-To-Refresh 源码解析
- 【框架学习】【android-Ultra-Pull-To-Refresh】
- android-Ultra-Pull-To-Refresh 源码解析
- android-Ultra-Pull-To-Refresh 源码解析
- android-Ultra-Pull-To-Refresh 源码解析
- 4.5.3 ULtra Pull To Refresh
- android-Ultra-Pull-To-Refresh 源码解析
- android-Ultra-Pull-To-Refresh 源码解析
- android-Ultra-Pull-To-Refresh 使用简介
- android-Ultra-Pull-To-Refresh 源码解析
- JS中常用到的数组工具方法
- 模运算的规则
- HDU/HDOJ 1004 Let the Balloon Rise(颜色统计)
- 通州十三五规划精华版:增几十万就业机会/一批地铁
- 关于Xcode编译性能优化的研究工作总结
- Ultra-Pull-To-Refresh动态添加View不能正常显示BUG解决方法
- js时间段列表生成方法
- Android自定义捕获Application全局异常
- hdu 1281 棋盘游戏 二分图匹配 匈牙利算法 暴力
- JAVA中的阻塞队列
- HelloWorld Driver
- 欢迎使用CSDN-markdown编辑器
- Apache OFBiz 学习笔记 之 服务引擎 二
- javascript中的||操作符与&&操作符