ExpandableListView 调用 getGroupView 时总是将 groupPosition 参数设为 0
ExpandableListView calls getGroupView always with groupPosition parameter as 0
我有一个虚拟 JSON 数据,它有一个大小为 8 的 ArrayList。它们必须填充在 expandableListView 中,但它只显示第一组。因为当我调试它时,getGroupView 总是在 groupPosition 参数为零的情况下被调用。
有很多关于用 ArrayLists 填充 ExpandableListViews 的例子,我的代码与它们几乎相同。但是我不明白为什么会这样。
任何帮助将不胜感激
我的 ExpandableListViewApdater:
public class ElvProgramCourseListAdapter extends BaseExpandableListAdapter {
private Activity activity;
private ProgramOnly programOnly;
public ElvProgramCourseListAdapter(Activity activity, ProgramOnly programOnly) {
this.activity = activity;
this.programOnly = programOnly;
}
@Override
public int getGroupCount() {
return programOnly.getProgramSummary().getProgramSetList().size();
}
@Override
public int getChildrenCount(int groupPosition) {
NativeProgramSet item = programOnly.getProgramSummary().getProgramSetList().get(groupPosition);
if (item.getId() != null) {
return item.getCourseIdList().size();
} else {
return 0;
}
}
@Override
public Object getGroup(int groupPosition) {
return programOnly.getProgramSummary().getProgramSetList().get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
NativeProgramSet item = programOnly.getProgramSummary().getProgramSetList().get(groupPosition);
if (item.getId() != null) {
return item.getCourseIdList().get(childPosition);
} else {
return null;
}
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ImageView imgContentListThumbnail;
ImageView imgContentListRibbon;
ImageView imgTLIcon;
ImageView imgKilitIcon;
TextView txtContentListTitle;
TextView txtContentListDescription;
ImageView imgContentListDetail;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.item_contentlist, parent, false);
imgContentListThumbnail = (ImageView) convertView.findViewById(R.id.imgContentListThumbnail);
imgContentListRibbon = (ImageView) convertView.findViewById(R.id.imgContentListRibbon);
imgTLIcon = (ImageView) convertView.findViewById(R.id.imgTLIcon);
imgKilitIcon = (ImageView) convertView.findViewById(R.id.imgKilitIcon);
txtContentListTitle = (TextView) convertView.findViewById(R.id.txtContentListTitle);
Fonts.setTypeface(Fonts.BOLD, txtContentListTitle, activity.getBaseContext());
txtContentListDescription = (TextView) convertView.findViewById(R.id.txtContentListDescription);
imgContentListDetail = (ImageView) convertView.findViewById(R.id.imgContentListDetail);
convertView.setTag(new ViewHolder(imgContentListThumbnail, imgContentListRibbon, imgTLIcon, imgKilitIcon, txtContentListTitle, txtContentListDescription, imgContentListDetail));
} else {
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
imgContentListThumbnail = viewHolder.imgContentListThumbnail;
imgContentListRibbon = viewHolder.imgContentListRibbon;
imgTLIcon = viewHolder.imgTLIcon;
imgKilitIcon = viewHolder.imgKilitIcon;
txtContentListTitle = viewHolder.txtContentListTitle;
txtContentListDescription = viewHolder.txtContentListDescription;
imgContentListDetail = viewHolder.imgContentListDetail;
}
NativeProgramSet nativeProgramSet = (NativeProgramSet) getGroup(groupPosition);
if(nativeProgramSet.getId() != null) {
convertView.setBackgroundColor(Color.parseColor("#e5e5e5"));
imgContentListThumbnail.setVisibility(View.GONE);
imgContentListRibbon.setVisibility(View.GONE);
imgTLIcon.setVisibility(View.GONE);
imgKilitIcon.setVisibility(View.GONE);
txtContentListDescription.setVisibility(View.GONE);
if(isExpanded) {
imgContentListDetail.setImageResource(R.drawable.accordion_close_icon);
} else {
imgContentListDetail.setImageResource(R.drawable.accordion_open_icon);
}
txtContentListTitle.setText(nativeProgramSet.getName());
} else {
convertView.setBackgroundColor(Color.WHITE);
imgContentListThumbnail.setVisibility(View.VISIBLE);
txtContentListDescription.setVisibility(View.VISIBLE);
imgContentListDetail.setImageResource(R.drawable.top_search_filter_button_icon);
Course course = null;
for(Course item : programOnly.getProgramSummary().getCourseList()) {
if(item.getId().equals(nativeProgramSet.getCourseIdList().get(0))) {
course = item;
break;
}
}
if(course != null) {
convertView = fillListItem(course, imgContentListThumbnail, imgContentListRibbon, imgTLIcon, imgKilitIcon, txtContentListTitle, txtContentListDescription, imgContentListDetail, convertView);
}
}
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ImageView imgContentListThumbnail;
ImageView imgContentListRibbon;
ImageView imgTLIcon;
ImageView imgKilitIcon;
TextView txtContentListTitle;
TextView txtContentListDescription;
ImageView imgContentListDetail;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.item_contentlist, parent, false);
imgContentListThumbnail = (ImageView) convertView.findViewById(R.id.imgContentListThumbnail);
imgContentListRibbon = (ImageView) convertView.findViewById(R.id.imgContentListRibbon);
imgTLIcon = (ImageView) convertView.findViewById(R.id.imgTLIcon);
imgKilitIcon = (ImageView) convertView.findViewById(R.id.imgKilitIcon);
txtContentListTitle = (TextView) convertView.findViewById(R.id.txtContentListTitle);
Fonts.setTypeface(Fonts.BOLD, txtContentListTitle, activity.getBaseContext());
txtContentListDescription = (TextView) convertView.findViewById(R.id.txtContentListDescription);
imgContentListDetail = (ImageView) convertView.findViewById(R.id.imgContentListDetail);
convertView.setTag(new ViewHolder(imgContentListThumbnail, imgContentListRibbon, imgTLIcon, imgKilitIcon, txtContentListTitle, txtContentListDescription, imgContentListDetail));
} else {
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
imgContentListThumbnail = viewHolder.imgContentListThumbnail;
imgContentListRibbon = viewHolder.imgContentListRibbon;
imgTLIcon = viewHolder.imgTLIcon;
imgKilitIcon = viewHolder.imgKilitIcon;
txtContentListTitle = viewHolder.txtContentListTitle;
txtContentListDescription = viewHolder.txtContentListDescription;
imgContentListDetail = viewHolder.imgContentListDetail;
}
Long courseId = (Long) getChild(groupPosition, childPosition);
imgContentListThumbnail.setVisibility(View.VISIBLE);
txtContentListDescription.setVisibility(View.VISIBLE);
imgContentListDetail.setImageResource(R.drawable.top_search_filter_button_icon);
Course course = null;
for(Course item : programOnly.getProgramSummary().getCourseList()) {
if(item.getId().equals(courseId)) {
course = item;
break;
}
}
if(course != null) {
convertView = fillListItem(course, imgContentListThumbnail, imgContentListRibbon, imgTLIcon, imgKilitIcon, txtContentListTitle, txtContentListDescription, imgContentListDetail, convertView);
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private static class ViewHolder {
public final ImageView imgContentListThumbnail;
public final ImageView imgContentListRibbon;
public final ImageView imgTLIcon;
public final ImageView imgKilitIcon;
public final TextView txtContentListTitle;
public final TextView txtContentListDescription;
public final ImageView imgContentListDetail;
public ViewHolder(ImageView imgContentListThumbnail, ImageView imgContentListRibbon, ImageView imgTLIcon, ImageView imgKilitIcon, TextView txtContentListTitle, TextView txtContentListDescription, ImageView imgContentListDetail) {
this.imgContentListThumbnail = imgContentListThumbnail;
this.imgContentListRibbon = imgContentListRibbon;
this.imgTLIcon = imgTLIcon;
this.imgKilitIcon = imgKilitIcon;
this.txtContentListTitle = txtContentListTitle;
this.txtContentListDescription = txtContentListDescription;
this.imgContentListDetail = imgContentListDetail;
}
}
private View fillListItem(final Course crs, ImageView imgContentListThumbnail, ImageView imgContentListRibbon, ImageView imgTLIcon, ImageView imgKilitIcon, TextView txtContentListTitle, TextView txtContentListDescription, ImageView imgContentListDetail, View convertView) {
//long stuff
}
}
编辑
ProgramOnly.class
public class ProgramOnly implements Serializable {
/**
*
*/
private static final long serialVersionUID = 75459834905648086L;
private String HtmlContent;
private Course currentCourse;
private Program program;
private int videoCount;
private int quizCount;
private int eLearningCount;
private ArrayList<Page> orderedRelatedContent;
private String KALTURA_SESSION_KEY;
private String KALTURA_PREVIEW_SESSION_KEY;
private String videoId;
private boolean isPublicAccess;
private ArrayList<Document> contentDocuments;
private CourseStatusMap courseStatusMap;
private CertificateStatusMap certificateStatus;
private String programAttendeeId;
private String attendeeId;
private int pdfCount;
private int pptCount;
private int htmlCount;
private int audioCount;
private int interval;
private ProgramStatus contentStatus;
private String urlForSocialSharing;
private boolean isContentMustBePurchased;
public Program getProgramSummary() {
return program;
}
//getter setter stuff
我找到了解决方案。 Android 没有我想象的那么聪明。我的 ExpandableListView
在 ScrollView
中,这使得它的高度始终与一个项目相同。真是个错误!
所以我通过在设置适配器和 onGroupExpand
和 onGroupCollapse
之后测量总高度来破解 Android
设置适配器:
ExpandableListView elvProgramCourseList = (ExpandableListView) findViewById(R.id.elvProgramCourseList);
ElvProgramCourseListAdapter adapter = new ElvProgramCourseListAdapter(this, programOnly);
elvProgramCourseList.setGroupIndicator(null);
elvProgramCourseList.setChildIndicator(null);
elvProgramCourseList.setAdapter(adapter);
setExpandableListViewHeightBasedOnChildren(elvProgramCourseList, null);
onGroupExpand
:
elvProgramCourseList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
ExpandableListView elvProgramCourseList = (ExpandableListView) findViewById(R.id.elvProgramCourseList);
collapseOtherGroups(elvProgramCourseList, groupPosition);
setExpandableListViewHeightBasedOnChildren(elvProgramCourseList, groupPosition);
}
});
onGroupCollapse
:
elvProgramCourseList.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.elvProgramCourseList);
ExpandableListAdapter expandableListAdapter = expandableListView.getExpandableListAdapter();
if (expandableListAdapter == null) {
return;
}
boolean areAllGroupsCollapsed = true;
for (int i = 0; i < expandableListAdapter.getGroupCount(); i++) {
if(expandableListView.isGroupExpanded(i)) {
areAllGroupsCollapsed = false;
break;
}
}
if(areAllGroupsCollapsed) {
setExpandableListViewHeightBasedOnChildren(expandableListView, null);
}
}
});
测量总高度:
private void setExpandableListViewHeightBasedOnChildren(ExpandableListView expandableListView, Integer expandedGroupPosition) {
ExpandableListAdapter expandableListAdapter = expandableListView.getExpandableListAdapter();
if (expandableListAdapter == null) {
return;
}
int totalHeight = 0;
int totalDividerHeight = 0;
for (int i = 0; i < expandableListAdapter.getGroupCount(); i++) {
View groupItem = expandableListAdapter.getGroupView(i, expandedGroupPosition != null, null, expandableListView);
totalHeight += Utils.convertDpToPixel(92.42f, this);
if(expandedGroupPosition != null && expandedGroupPosition.equals(i)) {
for(int j=0;j<expandableListAdapter.getChildrenCount(i);j++) {
View childItem = expandableListAdapter.getChildView(i, j, j+1==expandableListAdapter.getChildrenCount(i), null, expandableListView);
totalHeight += Utils.convertDpToPixel(92.42f, this);
}
totalDividerHeight += expandableListView.getDividerHeight() * (expandableListAdapter.getChildrenCount(i)-1);
}
}
totalDividerHeight += expandableListView.getDividerHeight() * (expandableListAdapter.getGroupCount()-1);
ViewGroup.LayoutParams params = expandableListView.getLayoutParams();
params.height = totalHeight + totalDividerHeight;
expandableListView.setLayoutParams(params);
expandableListView.requestLayout();
}
Android,你确实让我浪费了很多时间。 GRRRRRR!
我在教程中找到了另一个解决方案:
public class SecondLevelExpanableListView extends ExpandableListView {
public SecondLevelExpanableListView(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
这对我有用!
我有一个虚拟 JSON 数据,它有一个大小为 8 的 ArrayList。它们必须填充在 expandableListView 中,但它只显示第一组。因为当我调试它时,getGroupView 总是在 groupPosition 参数为零的情况下被调用。
有很多关于用 ArrayLists 填充 ExpandableListViews 的例子,我的代码与它们几乎相同。但是我不明白为什么会这样。
任何帮助将不胜感激
我的 ExpandableListViewApdater:
public class ElvProgramCourseListAdapter extends BaseExpandableListAdapter {
private Activity activity;
private ProgramOnly programOnly;
public ElvProgramCourseListAdapter(Activity activity, ProgramOnly programOnly) {
this.activity = activity;
this.programOnly = programOnly;
}
@Override
public int getGroupCount() {
return programOnly.getProgramSummary().getProgramSetList().size();
}
@Override
public int getChildrenCount(int groupPosition) {
NativeProgramSet item = programOnly.getProgramSummary().getProgramSetList().get(groupPosition);
if (item.getId() != null) {
return item.getCourseIdList().size();
} else {
return 0;
}
}
@Override
public Object getGroup(int groupPosition) {
return programOnly.getProgramSummary().getProgramSetList().get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
NativeProgramSet item = programOnly.getProgramSummary().getProgramSetList().get(groupPosition);
if (item.getId() != null) {
return item.getCourseIdList().get(childPosition);
} else {
return null;
}
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ImageView imgContentListThumbnail;
ImageView imgContentListRibbon;
ImageView imgTLIcon;
ImageView imgKilitIcon;
TextView txtContentListTitle;
TextView txtContentListDescription;
ImageView imgContentListDetail;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.item_contentlist, parent, false);
imgContentListThumbnail = (ImageView) convertView.findViewById(R.id.imgContentListThumbnail);
imgContentListRibbon = (ImageView) convertView.findViewById(R.id.imgContentListRibbon);
imgTLIcon = (ImageView) convertView.findViewById(R.id.imgTLIcon);
imgKilitIcon = (ImageView) convertView.findViewById(R.id.imgKilitIcon);
txtContentListTitle = (TextView) convertView.findViewById(R.id.txtContentListTitle);
Fonts.setTypeface(Fonts.BOLD, txtContentListTitle, activity.getBaseContext());
txtContentListDescription = (TextView) convertView.findViewById(R.id.txtContentListDescription);
imgContentListDetail = (ImageView) convertView.findViewById(R.id.imgContentListDetail);
convertView.setTag(new ViewHolder(imgContentListThumbnail, imgContentListRibbon, imgTLIcon, imgKilitIcon, txtContentListTitle, txtContentListDescription, imgContentListDetail));
} else {
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
imgContentListThumbnail = viewHolder.imgContentListThumbnail;
imgContentListRibbon = viewHolder.imgContentListRibbon;
imgTLIcon = viewHolder.imgTLIcon;
imgKilitIcon = viewHolder.imgKilitIcon;
txtContentListTitle = viewHolder.txtContentListTitle;
txtContentListDescription = viewHolder.txtContentListDescription;
imgContentListDetail = viewHolder.imgContentListDetail;
}
NativeProgramSet nativeProgramSet = (NativeProgramSet) getGroup(groupPosition);
if(nativeProgramSet.getId() != null) {
convertView.setBackgroundColor(Color.parseColor("#e5e5e5"));
imgContentListThumbnail.setVisibility(View.GONE);
imgContentListRibbon.setVisibility(View.GONE);
imgTLIcon.setVisibility(View.GONE);
imgKilitIcon.setVisibility(View.GONE);
txtContentListDescription.setVisibility(View.GONE);
if(isExpanded) {
imgContentListDetail.setImageResource(R.drawable.accordion_close_icon);
} else {
imgContentListDetail.setImageResource(R.drawable.accordion_open_icon);
}
txtContentListTitle.setText(nativeProgramSet.getName());
} else {
convertView.setBackgroundColor(Color.WHITE);
imgContentListThumbnail.setVisibility(View.VISIBLE);
txtContentListDescription.setVisibility(View.VISIBLE);
imgContentListDetail.setImageResource(R.drawable.top_search_filter_button_icon);
Course course = null;
for(Course item : programOnly.getProgramSummary().getCourseList()) {
if(item.getId().equals(nativeProgramSet.getCourseIdList().get(0))) {
course = item;
break;
}
}
if(course != null) {
convertView = fillListItem(course, imgContentListThumbnail, imgContentListRibbon, imgTLIcon, imgKilitIcon, txtContentListTitle, txtContentListDescription, imgContentListDetail, convertView);
}
}
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ImageView imgContentListThumbnail;
ImageView imgContentListRibbon;
ImageView imgTLIcon;
ImageView imgKilitIcon;
TextView txtContentListTitle;
TextView txtContentListDescription;
ImageView imgContentListDetail;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.item_contentlist, parent, false);
imgContentListThumbnail = (ImageView) convertView.findViewById(R.id.imgContentListThumbnail);
imgContentListRibbon = (ImageView) convertView.findViewById(R.id.imgContentListRibbon);
imgTLIcon = (ImageView) convertView.findViewById(R.id.imgTLIcon);
imgKilitIcon = (ImageView) convertView.findViewById(R.id.imgKilitIcon);
txtContentListTitle = (TextView) convertView.findViewById(R.id.txtContentListTitle);
Fonts.setTypeface(Fonts.BOLD, txtContentListTitle, activity.getBaseContext());
txtContentListDescription = (TextView) convertView.findViewById(R.id.txtContentListDescription);
imgContentListDetail = (ImageView) convertView.findViewById(R.id.imgContentListDetail);
convertView.setTag(new ViewHolder(imgContentListThumbnail, imgContentListRibbon, imgTLIcon, imgKilitIcon, txtContentListTitle, txtContentListDescription, imgContentListDetail));
} else {
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
imgContentListThumbnail = viewHolder.imgContentListThumbnail;
imgContentListRibbon = viewHolder.imgContentListRibbon;
imgTLIcon = viewHolder.imgTLIcon;
imgKilitIcon = viewHolder.imgKilitIcon;
txtContentListTitle = viewHolder.txtContentListTitle;
txtContentListDescription = viewHolder.txtContentListDescription;
imgContentListDetail = viewHolder.imgContentListDetail;
}
Long courseId = (Long) getChild(groupPosition, childPosition);
imgContentListThumbnail.setVisibility(View.VISIBLE);
txtContentListDescription.setVisibility(View.VISIBLE);
imgContentListDetail.setImageResource(R.drawable.top_search_filter_button_icon);
Course course = null;
for(Course item : programOnly.getProgramSummary().getCourseList()) {
if(item.getId().equals(courseId)) {
course = item;
break;
}
}
if(course != null) {
convertView = fillListItem(course, imgContentListThumbnail, imgContentListRibbon, imgTLIcon, imgKilitIcon, txtContentListTitle, txtContentListDescription, imgContentListDetail, convertView);
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private static class ViewHolder {
public final ImageView imgContentListThumbnail;
public final ImageView imgContentListRibbon;
public final ImageView imgTLIcon;
public final ImageView imgKilitIcon;
public final TextView txtContentListTitle;
public final TextView txtContentListDescription;
public final ImageView imgContentListDetail;
public ViewHolder(ImageView imgContentListThumbnail, ImageView imgContentListRibbon, ImageView imgTLIcon, ImageView imgKilitIcon, TextView txtContentListTitle, TextView txtContentListDescription, ImageView imgContentListDetail) {
this.imgContentListThumbnail = imgContentListThumbnail;
this.imgContentListRibbon = imgContentListRibbon;
this.imgTLIcon = imgTLIcon;
this.imgKilitIcon = imgKilitIcon;
this.txtContentListTitle = txtContentListTitle;
this.txtContentListDescription = txtContentListDescription;
this.imgContentListDetail = imgContentListDetail;
}
}
private View fillListItem(final Course crs, ImageView imgContentListThumbnail, ImageView imgContentListRibbon, ImageView imgTLIcon, ImageView imgKilitIcon, TextView txtContentListTitle, TextView txtContentListDescription, ImageView imgContentListDetail, View convertView) {
//long stuff
}
}
编辑
ProgramOnly.class
public class ProgramOnly implements Serializable {
/**
*
*/
private static final long serialVersionUID = 75459834905648086L;
private String HtmlContent;
private Course currentCourse;
private Program program;
private int videoCount;
private int quizCount;
private int eLearningCount;
private ArrayList<Page> orderedRelatedContent;
private String KALTURA_SESSION_KEY;
private String KALTURA_PREVIEW_SESSION_KEY;
private String videoId;
private boolean isPublicAccess;
private ArrayList<Document> contentDocuments;
private CourseStatusMap courseStatusMap;
private CertificateStatusMap certificateStatus;
private String programAttendeeId;
private String attendeeId;
private int pdfCount;
private int pptCount;
private int htmlCount;
private int audioCount;
private int interval;
private ProgramStatus contentStatus;
private String urlForSocialSharing;
private boolean isContentMustBePurchased;
public Program getProgramSummary() {
return program;
}
//getter setter stuff
我找到了解决方案。 Android 没有我想象的那么聪明。我的 ExpandableListView
在 ScrollView
中,这使得它的高度始终与一个项目相同。真是个错误!
所以我通过在设置适配器和 onGroupExpand
和 onGroupCollapse
设置适配器:
ExpandableListView elvProgramCourseList = (ExpandableListView) findViewById(R.id.elvProgramCourseList);
ElvProgramCourseListAdapter adapter = new ElvProgramCourseListAdapter(this, programOnly);
elvProgramCourseList.setGroupIndicator(null);
elvProgramCourseList.setChildIndicator(null);
elvProgramCourseList.setAdapter(adapter);
setExpandableListViewHeightBasedOnChildren(elvProgramCourseList, null);
onGroupExpand
:
elvProgramCourseList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
ExpandableListView elvProgramCourseList = (ExpandableListView) findViewById(R.id.elvProgramCourseList);
collapseOtherGroups(elvProgramCourseList, groupPosition);
setExpandableListViewHeightBasedOnChildren(elvProgramCourseList, groupPosition);
}
});
onGroupCollapse
:
elvProgramCourseList.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.elvProgramCourseList);
ExpandableListAdapter expandableListAdapter = expandableListView.getExpandableListAdapter();
if (expandableListAdapter == null) {
return;
}
boolean areAllGroupsCollapsed = true;
for (int i = 0; i < expandableListAdapter.getGroupCount(); i++) {
if(expandableListView.isGroupExpanded(i)) {
areAllGroupsCollapsed = false;
break;
}
}
if(areAllGroupsCollapsed) {
setExpandableListViewHeightBasedOnChildren(expandableListView, null);
}
}
});
测量总高度:
private void setExpandableListViewHeightBasedOnChildren(ExpandableListView expandableListView, Integer expandedGroupPosition) {
ExpandableListAdapter expandableListAdapter = expandableListView.getExpandableListAdapter();
if (expandableListAdapter == null) {
return;
}
int totalHeight = 0;
int totalDividerHeight = 0;
for (int i = 0; i < expandableListAdapter.getGroupCount(); i++) {
View groupItem = expandableListAdapter.getGroupView(i, expandedGroupPosition != null, null, expandableListView);
totalHeight += Utils.convertDpToPixel(92.42f, this);
if(expandedGroupPosition != null && expandedGroupPosition.equals(i)) {
for(int j=0;j<expandableListAdapter.getChildrenCount(i);j++) {
View childItem = expandableListAdapter.getChildView(i, j, j+1==expandableListAdapter.getChildrenCount(i), null, expandableListView);
totalHeight += Utils.convertDpToPixel(92.42f, this);
}
totalDividerHeight += expandableListView.getDividerHeight() * (expandableListAdapter.getChildrenCount(i)-1);
}
}
totalDividerHeight += expandableListView.getDividerHeight() * (expandableListAdapter.getGroupCount()-1);
ViewGroup.LayoutParams params = expandableListView.getLayoutParams();
params.height = totalHeight + totalDividerHeight;
expandableListView.setLayoutParams(params);
expandableListView.requestLayout();
}
Android,你确实让我浪费了很多时间。 GRRRRRR!
我在教程中找到了另一个解决方案:
public class SecondLevelExpanableListView extends ExpandableListView {
public SecondLevelExpanableListView(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
这对我有用!