android ListView 報錯 The specified child already has a parent

今天遇到一個問題,在listviev中加入傳進來的佈局,上下滑動時,報錯The specified child already has a parent。

先貼原始代碼:

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			convertView = layoutInflater.inflate(R.layout.check_laizi_item_ll, null);
			RelativeLayout mRelativeLayout = (RelativeLayout) convertView.findViewById(R.id.clil_rl);
			List poker = mPokers.get(position);
			for (int j = 0; j < poker.size(); j++) {
				Poker card = poker.get(j);
				if (card.isLaiZi()) {
					card.getPokeImage().setImageDrawable(ImageUtil.getResDrawable(card.getBitpamResLaiZiID(), true));
				} else {
					card.getPokeImage().setImageDrawable(ImageUtil.getResDrawable(card.getBitpamResID(), true));
				}
				card.setId(position + 100);
				RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mst.adjustXIgnoreDensity(45), mst.adjustYIgnoreDensity(61));
				params.topMargin = mst.adjustYIgnoreDensity(card_jiange);
				params.leftMargin = mst.adjustXIgnoreDensity(card_jiange * j);
				card.getInnerLayout().setBackgroundDrawable(ImageUtil.getResDrawable(R.drawable.poker_p, true));
				card.getInnerLayout().setVisibility(View.GONE);
				card.setClickable(false);
				card.setLayoutParams(params);
				mRelativeLayout.addView(card);
			}

			return convertView;
		}

再貼圖片:

原因是listview中我的Poker控件被多個convertView引用。

之前以為是我的convertView框架被復用的原因,因此我不對convertView進行服用,每次都創建一個convertView,可是也會報這個錯誤。

我想問題應該在position上,具體神馬情況不是我也不好解釋,隻是感覺position與convertView的變動沒有及時同步吧vcD4KPHA+xMfO0r7NsNFjb252ZXJ0Vmlld9PDyv3X6bGjtObG8MC0o6zIu7rzuPm+3XBvc2l0aW9uyKXIoaOs1eLR+crHy/vDx82ssr08L3A+CjxwPjxicj4KPC9wPgo8cD7U2cz50N64xLrztPrC66O6PC9wPgo8cD48cHJlIGNsYXNzPQ==”brush:java;”> @Override
public View getView(int position, View convertView, ViewGroup parent) {

if(null==convertViews[position]){
convertView = layoutInflater.inflate(R.layout.check_laizi_item_ll, null);
RelativeLayout mRelativeLayout = (RelativeLayout) convertView.findViewById(R.id.clil_rl);
List poker = mPokers.get(position);
for (int j = 0; j < poker.size(); j++) {
Poker card = poker.get(j);
if (card.isLaiZi()) {
card.getPokeImage().setImageDrawable(ImageUtil.getResDrawable(card.getBitpamResLaiZiID(), true));
} else {
card.getPokeImage().setImageDrawable(ImageUtil.getResDrawable(card.getBitpamResID(), true));
}
card.setId(position + 100);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mst.adjustXIgnoreDensity(45), mst.adjustYIgnoreDensity(61));
params.topMargin = mst.adjustYIgnoreDensity(card_jiange);
params.leftMargin = mst.adjustXIgnoreDensity(card_jiange * j);
card.getInnerLayout().setBackgroundDrawable(ImageUtil.getResDrawable(R.drawable.poker_p, true));
card.getInnerLayout().setVisibility(View.GONE);
card.setClickable(false);
card.setLayoutParams(params);
mRelativeLayout.addView(card);
convertViews[position]=convertView;
}
}else{
convertView=convertViews[position];
}
Log.i("positionss", "position:"+position+" mPokers.size:"+mPokers.size()+" convertViews[position]:"+convertViews[position]);
return convertView;
}

發佈留言