如何使用自定义 ExpandedListView 垂直扩展或填充屏幕
How to vertically extend or fill the screen with custom ExpandedListView
我有一个自定义 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);
嗯,其实我找到了一些解决办法。
我已将 ExpandableListView
放入我的 RelativeLayout
中 activity_main
。
并在MainActivity:
中改变了实现
ExpandableListView list = (ExpandableListView) findViewById(R.id.expandableListView_Parent);
if (list != null) {
ParentAdapter adapter = new ParentAdapter(this, objectsLvl1);
list.setAdapter(adapter);
}
这解决了我的问题,现在我有了全屏 ExpandableList
。
但是,如果有人告诉我该怎么做,如果我想通过我自己的 CustomExpandableList
来做,那就太好了。因为我还是不知道怎么做。
我有一个自定义 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);
嗯,其实我找到了一些解决办法。
我已将 ExpandableListView
放入我的 RelativeLayout
中 activity_main
。
并在MainActivity:
中改变了实现ExpandableListView list = (ExpandableListView) findViewById(R.id.expandableListView_Parent);
if (list != null) {
ParentAdapter adapter = new ParentAdapter(this, objectsLvl1);
list.setAdapter(adapter);
}
这解决了我的问题,现在我有了全屏 ExpandableList
。
但是,如果有人告诉我该怎么做,如果我想通过我自己的 CustomExpandableList
来做,那就太好了。因为我还是不知道怎么做。