我编写了BaseExpandableListAdapter的实现,它还覆盖了getChildType和getChildView方法。有10个小组,其中4个有孩子。
以下实施在模拟器和测试的htc设备上运行。这意味着:
当我以可视方式打开GROUP_W时,getChildView(...)的convertView参数在第一次正确为null并稍后重新使用该视图。
此实施在Samsungs Galaxy S3和Galaxy Tab上无效。这意味着: 当我以可视方式打开GROUP_W时,getChildView(...)的convertView参数第一次不为空,并提供错误的视图,从而导致异常。
@Override
public int getChildType(int groupPosition, int childPosition) {
switch (groupPosition) {
case GROUP_W:
return 0;
case GROUP_X:
case GROUP_Y:
return 1;
case GROUP_Z:
return 2;
default:
// actually not used
return super.getChildType(groupPosition, childPosition);
}
}
@Override
public int getChildTypeCount() {
return 3;
}
当我将实现更改为以下内容时,没有看到任何逻辑原因,那么它也适用于三星设备。
@Override
public int getChildType(int groupPosition, int childPosition) {
switch (groupPosition) {
case GROUP_W:
return 1;
case GROUP_X:
case GROUP_Y:
return 2;
case GROUP_Z:
return 3;
default:
return super.getChildType(groupPosition, childPosition);
}
}
@Override
public int getChildTypeCount() {
return 4;
}
是否有人也有这种奇怪的行为,或者有人知道我在做什么或出了什么问题? 非常感谢;)
编辑:添加了getChildView()
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
Context context = parent.getContext();
switch (groupPosition) {
case GROUP_W:
X x = arrayX[childPosition];
return ListItemFactory.getTwoLineListItemTextIndicatorTopRight(context, x.getString1(), x.getString2(), x.getString3(), convertView);
case ...
default:
return null;
}
}
public static View getTwoLineListItemTextIndicatorTopRight(Context context, CharSequence line1, CharSequence line2,
CharSequence indicator, View convertView) {
View view;
if (convertView == null) {
view = View.inflate(context, R.layout.two_line_list_item_textindicator_topright, null);
}
else {
view = convertView;
}
// here happens a null pointer exception because textView1 is not available in the wrong convert view
((TextView) view.findViewById(R.id.textView1)).setText(line1);
((TextView) view.findViewById(R.id.textView2)).setText(line2);
if (indicator == null || indicator == "") {
((TextView) view.findViewById(R.id.textViewIndicator)).setVisibility(View.GONE);
}
else {
TextView textView = ((TextView) view.findViewById(R.id.textViewIndicator));
textView.setText(indicator);
textView.setVisibility(View.VISIBLE);
}
return view;
}