我相信问题出在你的getChildView()
方法上:
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.catitem, parent, false);
TextView textView_catName = (TextView)convertView.findViewById(R.id.textView_catName);
Category current = categories.get(groupPosition).childs.get(childPosition);
textView_catName.setText(groupPosition + " , " + childPosition);
if(current.childs.size() > 0 ) {
ExpandableListView elv = new ExpandableListView(context);
elv.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT));
elv.setAdapter(new CatAdapter(context, current.childs));
((ViewGroup)convertView).addView(elv);
}
return convertView;
}
当你遇到一个“可扩展”的孩子时,你仍然在膨胀R.layout.catitem
并添加你的新孩子elv
。由于catitem
是 aRelativeLayout
并且您没有添加任何对齐参数,因此每个视图都放置在左上角,覆盖那里的任何其他内容。
您可能想尝试更改R.layout.catitem
以将垂直LinearLayout
作为其根。这应该可以防止他们重叠孩子的标题,但我不能保证孩子的孩子仍然不会重叠。不过,这是一个简单的改变,值得一试。
此外,来自文档ExpandableListView
:
注意:如果父级的大小也没有严格指定(例如,如果父级是 ScrollView,则不能指定 wrap_content,因为它也可以是任何长度。但是,如果 ExpandableListView 父级具有特定大小(例如 100 像素),则可以使用 wrap_content。
那就是“在 XML 中”,所以我不确定这是否意味着适用于代码。在我看来,他们会使用相同的机制,因此可能值得研究。我不确定您将如何为父母设置最大尺寸。您也许可以测量一件物品的大小,然后乘以孩子的数量。您必须考虑边距/分隔符,所以它可能并不简单。
如果这不起作用,您可能需要直接滚动您自己的 ViewGroup。从头开始实现它应该不会太难,只是不要忘记尝试利用视图回收(您当前的方法无论如何都不会这样做)。