我想为每个组显示不同的子布局,例如:
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
switch (groupPosition){
case 0:
LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_row, null);
TextView tvPlayerName = (TextView) convertView.findViewById(R.id.tvPlayerName);
tvPlayerName.setText(arrChildelements[groupPosition][childPosition]);
break;
case 1:
LayoutInflater inflater1 = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater1.inflate(R.layout.child_row1, null);
TextView tvPlayerName1 = (TextView) convertView.findViewById(R.id.tvPlayerName);
tvPlayerName1.setText(arrChildelements[groupPosition][childPosition]);
break;
case 2:
LayoutInflater inflater2 = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater2.inflate(R.layout.child_row2, null);
TextView tvPlayerName2 = (TextView) convertView.findViewById(R.id.tvPlayerName);
tvPlayerName2.setText(arrChildelements[groupPosition][childPosition]);
break;
case 3:
LayoutInflater inflater3 = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater3.inflate(R.layout.child_row3, null);
TextView tvPlayerName3 = (TextView) convertView.findViewById(R.id.tvPlayerName);
tvPlayerName3.setText(arrChildelements[groupPosition][childPosition]);
break;
}
} return convertView;
}
我的问题是在点击群组时,子列表在群组之间相互交换。
有人可以告诉我,我做错了吗?
答案 0 :(得分:7)
这是因为
if (convertView == null)
删除它,它应该工作正常,如果它在删除这个if语句后应该工作,返回它并在if(convertView == null)中添加if语句,使其显示为你想要的,因为删除if( convertView == null)会使滚动速度变得更慢并且使用更多ram。
例如,您可以将所有视图放在一个xml文件或一个视图中,然后在switch方法中使用setVisibility来显示适合当前列表项的布局部分。答案 1 :(得分:3)
if(convertView == null)
在子视图中从自定义适配器中删除
它适用于所有