Listview嵌套gridview条目,在Listview的Adapter中实现gridview的条目点击notifyDataSetChanged()失效
发布时间:2016-12-14 来源:查字典编辑
摘要:这是Listview的Adapter.adapter.notifyDataSetChanged();方法失效/***商品详情sku适配器*C...
这是Listview的Adapter.adapter.notifyDataSetChanged();方法失效
/** *商品详情sku适配器 *Createdbykxxon2016/12/10. */ publicclassGoodsDetailSkuAdapterextendsBaseAdapter{ privatefinalActivitycontext; ListskuArray; privateGoodsDetailSkuGridAdapteradapter; privateintsize=0; privateintcolor=0; privateintstyle=0; //接口回调 SkuInterfaceskuListener; publicGoodsDetailSkuAdapter(Activitycontext,ListskuArray,SkuInterfaceskuListener){ this.skuArray=skuArray; this.context=context; this.skuListener=skuListener; } @Override publicintgetCount(){ returnskuArray.size(); } @Override publicObjectgetItem(intposition){ returnnull; } @Override publiclonggetItemId(intposition){ return0; } @Override publicViewgetView(finalintposition,ViewconvertView,ViewGroupparent){ ViewHolderholder; if(convertView==null){ holder=newViewHolder(); convertView=UiUtils.inflate(R.layout.item_goods_detail_sku); holder.tv_dialog_sku_title=(TextView)convertView.findViewById(R.id.tv_dialog_sku_title); holder.gv_dialog_properties=(MyGridView)convertView.findViewById(R.id.gv_dialog_properties); convertView.setTag(holder); }else{ holder=(ViewHolder)convertView.getTag(); } holder.tv_dialog_sku_title.setText(skuArray.get(position).name); adapter=newGoodsDetailSkuGridAdapter(skuArray.get(position).val); holder.gv_dialog_properties.setAdapter(adapter); holder.gv_dialog_properties.setOnItemClickListener(newAdapterView.OnItemClickListener(){ @Override publicvoidonItemClick(AdapterViewparent,Viewview,intsposition,longid){ if(skuArray.get(position).title.equals("chima")){ adapter.setClickPosition(sposition); size=Integer.parseInt(skuArray.get(position).val.get(sposition).id); intsizeId=skuArray.get(position).id; skuListener.setSkuSize(sizeId+":"+size); adapter.notifyDataSetChanged(); }elseif(skuArray.get(position).title.equals("yanse")){ adapter.setClickPosition(sposition); color=Integer.parseInt(skuArray.get(position).val.get(sposition).id); intcolorId=skuArray.get(position).id; skuListener.setSkuColor(colorId+":"+color); adapter.notifyDataSetChanged(); }elseif(skuArray.get(position).title.equals("kuanshi")){ adapter.setClickPosition(sposition); style=Integer.parseInt(skuArray.get(position).val.get(sposition).id); intstyleId=skuArray.get(position).id; skuListener.setSkuStyle(styleId+":"+style); adapter.notifyDataSetChanged(); } } }); returnconvertView; } privateclassViewHolder{ TextViewtv_dialog_sku_title; MyGridViewgv_dialog_properties; } }
这是gridview的adapter
/** *sku条目适配器 *Createdbykxxon2016/12/10. */ publicclassGoodsDetailSkuGridAdapterextendsBaseAdapter{ privateListval=newArrayList(); privateintclickPosition=-1; publicGoodsDetailSkuGridAdapter(Listval){ this.val=val; } @Override publicintgetCount(){ returnval.size(); } @Override publicObjectgetItem(intposition){ returnnull; } @Override publiclonggetItemId(intposition){ return0; } publicvoidsetClickPosition(intposition){ this.clickPosition=position; } @Override publicViewgetView(finalintposition,ViewconvertView,ViewGroupparent){ finalViewHolderholder; if(convertView==null){ holder=newViewHolder(); convertView=UiUtils.inflate(R.layout.item_sku_text_normal); holder.tv_name=(TextView)convertView.findViewById(R.id.tv_name); convertView.setTag(holder); }else{ holder=(ViewHolder)convertView.getTag(); } holder.tv_name.setText(val.get(position).rename); if(position==clickPosition){ //Toast.makeText(UiUtils.getContext(),"执行刷新",Toast.LENGTH_SHORT).show(); holder.tv_name.setBackgroundResource(R.drawable.shape_sku_text_pressed_bg); holder.tv_name.setTextColor(Color.WHITE); }else{ holder.tv_name.setBackgroundResource(R.drawable.shape_sku_text_normal_bg); holder.tv_name.setTextColor(Color.rgb(109,109,109)); } returnconvertView; } privateclassViewHolder{ publicTextViewtv_name; } }
其他方法都没事,只有adapter.notifyDataSetChanged();没有用。求各路大神给解决方法。
现在是要实现条目的点击效果,点击的地方变红,adapter.notifyDataSetChanged();这个方法不执行,无法刷新。
回复讨论(解决方案)
ListView的每个item都有一个GridView,所以GridView的Adapter如果写成ListView的Adapter的成员变量,那么ListView的上一个Item的GridView的Adapter就会被下一个Item的GridView的Adapter覆盖掉,所以当点击GridView的Item的时候去调用adapter.notify...的时候实际是调用了ListView的最后一个Item的GridView的Adapter.notify,所以你点记得时候adapter.notify实际是刷新了最后一个,所以看起来是无效的。
正确是方式应该是ListView的每个Item都要写成局部成员变量并且加final修饰,具体代码如下:
//privateGoodsDetailSkuGridAdapteradapter;//这一行代码注释了。 ... @Override publicViewgetView(finalintposition,ViewconvertView,ViewGroupparent){ ViewHolderholder; if(convertView==null){ holder=newViewHolder(); convertView=UiUtils.inflate(R.layout.item_goods_detail_sku); holder.tv_dialog_sku_title=(TextView)convertView.findViewById(R.id.tv_dialog_sku_title); holder.gv_dialog_properties=(MyGridView)convertView.findViewById(R.id.gv_dialog_properties); convertView.setTag(holder); }else{ holder=(ViewHolder)convertView.getTag(); } holder.tv_dialog_sku_title.setText(skuArray.get(position).name); finalGoodsDetailSkuGridAdapteradapter=newGoodsDetailSkuGridAdapter(skuArray.get(position).val); holder.gv_dialog_properties.setAdapter(adapter); holder.gv_dialog_properties.setOnItemClickListener(newAdapterView.OnItemClickListener(){ @Override publicvoidonItemClick(AdapterViewparent,Viewview,intsposition,longid){ if(skuArray.get(position).title.equals("chima")){ adapter.setClickPosition(sposition); size=Integer.parseInt(skuArray.get(position).val.get(sposition).id); intsizeId=skuArray.get(position).id; skuListener.setSkuSize(sizeId+":"+size); adapter.notifyDataSetChanged(); }elseif(skuArray.get(position).title.equals("yanse")){ adapter.setClickPosition(sposition); color=Integer.parseInt(skuArray.get(position).val.get(sposition).id); intcolorId=skuArray.get(position).id; skuListener.setSkuColor(colorId+":"+color); adapter.notifyDataSetChanged(); }elseif(skuArray.get(position).title.equals("kuanshi")){ adapter.setClickPosition(sposition); style=Integer.parseInt(skuArray.get(position).val.get(sposition).id); intstyleId=skuArray.get(position).id; skuListener.setSkuStyle(styleId+":"+style); adapter.notifyDataSetChanged(); } } }); returnconvertView; }