现在的位置: 首页 > 综合 > 正文

ListView 无法隐藏header footer视图的解决方法

2014年01月22日 ⁄ 综合 ⁄ 共 1028字 ⁄ 字号 评论关闭

经过测试发现:


LinearLayout footerLayout=(LinearLayout) inflater.inflate(R.layout.footer,null);

footerLayout.setVisibility(View.GONE);

通过这样的方法去隐藏listview的 header,footer视图都是没用的,于是打算隐藏header里面的子视图:


LinearLayout footerLayout=(LinearLayout) inflater.inflate(R.layout.footernull);
LinearLayout footerContentLayout=footerLayout.findViewById(R.id.footer_content_layout);

footerContentLayout.setVisibility(View.GONE);

使用了上面这段代码后就会出现如下异常:

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams

Caused by: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams

奇怪的是:footer_content_layout 这个ID 明明是一个LinearLayout类型的视图,但是异常却说我的对象转换不正确。

开始执行footerLayout.setVisibility(View.GONE); 不会出错,但是执行了footerContentLayout.setVisibility(View.GONE);后就挂了。

这异常让我想了好久都没能解决,突然二极管短路,我想起曾经也遇到过一模一样的情况,于是想起了以前的解决办法:


footerLayout.getChildAt(0).setVisibility(View.GONE);

就是这句解决了问题,理论上使用:

LinearLayout footerContentLayout=footerLayout.findViewById(R.id.footer_content_layout);
footerContentLayout.setVisibility(View.GONE);

这两句也是可以做到的,但是不明白为什么会出现类型转换异常,忘路过的大仙指点一二....

抱歉!评论已关闭.