解决条目布局中imageview个数不确定

在处理包含不确定数量ImageView的ListView条目时,需要确保在无图情况下释放空间,并防止因ListView复用机制导致的重复创建。通过在布局中预定义ImageView并在Adapter中动态显示或隐藏来实现。在getView()方法中设置各组件的值,并根据评论数据决定ImageView的可见性。
摘要由CSDN通过智能技术生成

当一个条目布局中imageview的个数不确定,特别是个数为零时,需要把这部分空间释放出去,这个时候如果动态创建imageview,由于启用了listview的复用机制,会导致imageview被重复创建,所以当利用了listview的复用机制时,在adapter不能new对象。这时候我们需要在条目布局中首先创建imageview,在adapter中有选择的显示和隐藏。Java代码如下:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder vh;
if(convertView==null){
convertView=inflater.inflate(R.layout.item_businessdetail_layout, parent,false);
vh=new ViewHolder();
vh.ivAvatar=(CircleImageView) convertView.findViewById(R.id.iv_detail_avatar);
vh.ivRating=(ImageView) convertView.findViewById(R.id.iv_detail_rating);
vh.ivPic1=(ImageView) convertView.findViewById(R.id.iv_detail_pic1);
vh.ivPic2=(ImageView) convertView.findViewById(R.id.iv_detail_pic2);
vh.ivPic3=(ImageView) convertView.findViewById(R.id.iv_detail_pic3);
vh.tvAvgprice=(TextView) convertView.findViewById(R.id.tv_detail_avgprice);
vh.tvUsername=(TextView) convertView.findViewById(R.id.tv_detail_username);
vh.tvContent=(TextView) convertView.findViewById(R.id.tv_detail_content);
convertView.setTag(vh);
}else{
vh=(ViewHolder) convertView.getTag();
}
Comment comment=getItem(position);
vh.tvUsername.setText(comment.getUsername());
vh.tvContent.setText(comment.getContent());
vh.tvAvgprice.setText("¥"+comment.getAvgprice()+"/人");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值