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

本文介绍了一种在尝试隐藏ListView Header内的子视图时遇到的ClassCastException异常现象,并给出了具体的解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

经过测试发现:


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);


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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值