我有一个包含多个组的 ExpandableList,每个组都包含一个不同类型的子项(不同的布局)。我有几个具有相似结构但子视图不同的活动,因此我决定为它编写一个自定义适配器。适配器在大多数情况下都运行良好,除了几个问题:
- 在子视图中键入文本几乎是不可能的。在键入每个字符后,EditText 似乎失去了焦点。我附上了一个截图来说明这种行为。
- 当组展开/折叠几次后,TextViews 中的文本似乎消失了,每个视图中只出现第一个单词的第一个字母。不知何故,它们的布局参数似乎以某种方式计算错误。
以下是截图:
这是我的适配器的代码:
public class GenericExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private String[] groups;
private View[] children;
public static interface ExpandableListItemClickListener {
public void onClick(View view, int groupId, int itemId);
}
public GenericExpandableListAdapter(Context context, String[] groups, int[] children) {
if(groups.length != children.length) {
throw new IllegalArgumentException("Items must have the same length as groups.");
}
this.context = context;
this.groups = groups;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.children = new View[children.length];
for(int i = 0; i < children.length; i++) {
this.children[i] = inflater.inflate(children[i], null);
}
}
public GenericExpandableListAdapter(Context context, String[] groups, View[] children) {
if(groups.length != children.length) {
throw new IllegalArgumentException("Items must have the same length as groups.");
}
this.context = context;
this.groups = groups;
this.children = children;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition];
}
@Override
public int getChildTypeCount() {
return children.length + 1;
}
@Override
public int getChildType(int groupPosition, int childPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if(convertView != null) {
return convertView;
}
return children[groupPosition];
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
@Override
public int getGroupTypeCount() {
return 1;
}
@Override
public int getGroupCount() {
return groups.length;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupType(int groupPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if(convertView != null) {
((TextView) convertView).setText(getGroup(groupPosition).toString());
return convertView;
}
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);
final TextView textView = new TextView(context);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(50, 0, 0, 0);
textView.setText(getGroup(groupPosition).toString());
return textView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}