public View getView(final int position, View convertView, ViewGroup parent) { final Shop shop =al.get(position); if(map.get(position)==null){ System.out.println("map 没有数据当前第"+position+"项"); viewholder=new ViewHolder(); viewholder.position=position; LayoutInflater inflater=activity.getLayoutInflater(); convertView=inflater.inflate(R.layout.main_item, null); viewholder.img=(ImageView)convertView.findViewById(R.id.img); viewholder.time=(TextView)convertView.findViewById(R.id.time); viewholder.tel=(TextView)convertView.findViewById(R.id.tel); viewholder.collect=(TextView)convertView.findViewById(R.id.collect); viewholder.name=(TextView)convertView.findViewById(R.id.name); viewholder.l1=(LinearLayout)convertView.findViewById(R.id.linearlayout_01); viewholder.l2=(LinearLayout)convertView.findViewById(R.id.linearlayout_02); viewholder.hot=(ImageView)convertView.findViewById(R.id.hot); viewholder.dayang=(ImageView)convertView.findViewById(R.id.dayang); viewholder.detail=(TextView)convertView.findViewById(R.id.detail); viewholder.menu=(TextView)convertView.findViewById(R.id.menu); //加载图片 LoadImageAsyncTask task=new LoadImageAsyncTask(); task.setImageView(viewholder.img); task.execute(url+shop.getPhotoPath()); //设置名字 viewholder.name.setText(shop.getName()); //设置收藏数 viewholder.collect.setText(shop.getCollect()); //设置电话 String tels=shop.getTel(); String []telarr=tels.split("%"); viewholder.tel.setText(telarr[0]); //设置时间 String times=shop.getTime(); String []timearr=times.split("%"); if(timearr.length==1){ viewholder.time.setText(timearr[0]); }else{ viewholder.time.setText(timearr[0]+"、"+timearr[1]); } map.put(position, convertView); convertView.setTag(viewholder); }else{ System.out.println("加载缓存的"+position); convertView=map.get(position); viewholder=(ViewHolder)convertView.getTag(); } String times=shop.getTime(); String []timearr=times.split("%"); if(timearr.length==1){ flag=timeutil.checkTime(timearr[0]); }else{ flag=timeutil.checkTime(timearr[0],timearr[1]); } final int currentPosition=position; if(flag){ if(position<3){ viewholder.hot.setVisibility(View.VISIBLE); } ((ViewHolder)map.get(currentPosition).getTag()).l1.setOnClickListener(new OnClickListener(){ boolean bool=true; @Override public void onClick(View v) { if(bool){ System.out.println("显示控件"); ((ViewHolder)map.get(currentPosition).getTag()).l2.setVisibility(View.VISIBLE); bool=false; }else{ System.out.println("隐藏控件"); ((ViewHolder)map.get(currentPosition).getTag()).l2.setVisibility(View.GONE); bool=true; } } }); viewholder.detail.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Intent intent=new Intent(); intent.setClass(activity,Detail_Activity.class); intent.putExtra("shopname", shop.getName()); activity.startActivity(intent); } }); viewholder.menu.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Intent intent=new Intent(); intent.setClass(activity,Menu_Activity.class); intent.putExtra("shopname", shop.getName()); activity.startActivity(intent); } }); }else{ viewholder.dayang.setVisibility(View.VISIBLE); } return convertView; }
下划线部分 绑定事件出错,具体描述是,点击r1 另外一个item 的r2显示了出来,添加了((ViewHolder)map.get(currentPosition).getTag()),这种情况没有了但是,有一些item不响应事件。我之前没有使用缓存的时候不存在这个问题。