【发布时间】:2016-05-10 13:03:31
【问题描述】:
我有一个自定义的 3 级 ExpandableListView 和 2 个自定义 ExpandableListAdapter。
在getChildView 内部的ParentAdapter 中,我创建了一个CustomExpandableListView 的实例,并将我的ChildAdapter 设置为这样:
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
CustomExpandableListView subObjects = (CustomExpandableListView) convertView;
if (convertView == null) {
subObjects = new CustomExpandableListView(activity);
}
ChildAdapter adapter = new ChildAdapter(activity, item, subObjects);
subObjects.setAdapter(adapter);
return subObjects;
}
部分截图:
当我的中国地区被隐藏时,这是我得到的:
还有这个展开后:
所以我的ExpandableListView 只占据了屏幕的一部分(我用红线标记了它),其他东西消失了,我必须滚动才能找到它。只有当我将元素添加到我的 Lvl 1(亚洲和欧洲)时,我的列表才会垂直扩展。我明白为什么 - 因为它们是标准的元素ExpandableListView
而且我知道如何删除上边距和左边距 - 我在布局中设置了填充。
但是我怎样才能强制我的CustomExpendableListView 也垂直扩展呢?
这是我的 CustomExpendableListView:
public class CustomExpandableListView extends ExpandableListView {
public CustomExpandableListView(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
widthMeasureSpec = MeasureSpec.makeMeasureSpec(990, MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(1600, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
在我的activity_main 布局中,我只有一个RelativeLayout,宽度和高度处于match_parent 状态。
这就是我在 MainActivity:
中初始化CustomExpandableListView 的方式
RelativeLayout parent = (RelativeLayout) findViewById(R.id.relativeContainer);
list = new CustomExpandableListView(this);
ParentAdapter adapter = new ParentAdapter(this, objectsLvl1);
list.setAdapter(adapter);
【问题讨论】:
标签: android android-layout expandablelistview