我有一个包含多个组的ExpandableList,每个组包含一个不同类型的子(不同的布局)。我有几个具有类似结构但有不同子视图的活动,所以我决定为它编写一个自定义适配器。适配器在大多数情况下运行良好,除了几个问题:
以下是截图:
这是我的适配器的代码:
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;
}
}
答案 0 :(得分:4)
我遇到了类似的问题。
在您的清单文件中添加它,看看它是否有效。它确实适合我。
android:windowSoftInputMode="adjustPan"
在您的活动代码中,所以它会是这样的
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustPan">