ListView中有控件隐藏或显示造成的显示错误问题

 

ListView中有控件隐藏或显示造成的显示错误问题

 

直接上代码

@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		/**
		 * 进行ListView 的优化
		 */
		if (convertView == null) {
			convertView = (ViewGroup) LayoutInflater.from(context).inflate(
					R.layout.abc_fragment_nearby_listview, parent, false);
			holder = new ViewHolder();
			holder.iv_type=(ImageView) convertView.findViewById(R.id.abc_fragment_nearby__listview__iv_type);
			holder.iv_faceimg=(ImageView) convertView.findViewById(R.id.abc_fragment_nearby__listview__iv_faceimg);
			holder.tv_name=(TextView) convertView.findViewById(R.id.abc_fragment_nearby_listview__tv_name);
			holder.iv_player_team_faceimg= (ImageView) convertView.findViewById(R.id.abc_fragment_nearby__listview__iv_player_team_faceimg);
			holder.tv_player_team_name= (TextView) convertView.findViewById(R.id.abc_fragment_nearby__listview__iv_player_team_name);
			convertView.setTag(holder);
			Log.d("TagListView", "新创建ViewHolder");
		} else {
			holder = (ViewHolder)convertView.getTag();
			Log.d("TagListView", "缓存中获取ViewHolder");
		}
		
		//restore view state
		holder.tv_player_team_name.setVisibility(View.VISIBLE);
		holder.iv_player_team_faceimg.setVisibility(View.VISIBLE);

		//get data
		final BeanNearby bean = datas.get(position);
		
		//数据类型
		int type=bean.getType();
		ImageView iv_type= holder.iv_type;
		int[] types = new int[]{R.drawable.abc_fragment_nearby__listview__iv_type0,R.drawable.abc_fragment_nearby__listview__iv_type1,R.drawable.abc_fragment_nearby__listview__iv_type2};
		iv_type.setImageResource(types[type]);
		
		//设置图片
		String faceimg = bean.getFaceimg();
		ImageView iv_faceimg= holder.iv_faceimg;
		logi("faceimg="+faceimg);
		
		//显示图片
		new DownAndShowImageTask(faceimg,iv_faceimg).execute();
		
		//设置名称
		String name = bean.getName();
		TextView tv_name = holder.tv_name;
		tv_name.setText(name);
		
		//如果是球员
		ImageView iv_player_team_faceimg = holder.iv_player_team_faceimg;
		TextView tv_player_team_name =holder.tv_player_team_name;
		String playerTeamName = bean.getPlayerTeamName();
		String playerTeamFaceimg = bean.getPlayerTeamFaceimg();
		if(type==BeanNearby.TYPE_PERSON){
			if(playerTeamName==null){
				playerTeamName="";
			}
			tv_player_team_name.setText(playerTeamName);
			
			if(playerTeamFaceimg!=null&&playerTeamFaceimg.length()>0){
				new DownAndShowImageTask(playerTeamFaceimg,iv_player_team_faceimg).execute();
			}else{
				iv_player_team_faceimg.setVisibility(View.INVISIBLE);
			}
		}else{
			tv_player_team_name.setVisibility(View.GONE);
			iv_player_team_faceimg.setVisibility(View.GONE);
		}

		return convertView;
	}

 

 其中的

//restore view state
holder.tv_player_team_name.setVisibility(View.VISIBLE);
holder.iv_player_team_faceimg.setVisibility(View.VISIBLE);

 这段代码必须要有,对布局进行还原。这是因为在下面的代码中会对控件进行隐藏(View.INVISIBLE)或彻底不显示(View.GONE)。

如果不加这段代码,这个ListView上下滚动时候你会发现数据会发生混乱和错误。

 

你可能感兴趣的:(ListView)