【问题标题】:How to vertically extend or fill the screen with custom ExpandedListView如何使用自定义 ExpandedListView 垂直扩展或填充屏幕
【发布时间】: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


    【解决方案1】:

    嗯,其实我已经找到了一些解决方案。

    我已将ExpandableListView 放入我的RelativeLayoutactivity_main

    并改变了 MainActivity 内部的实现:

    ExpandableListView list = (ExpandableListView) findViewById(R.id.expandableListView_Parent);
    
        if (list != null) {
            ParentAdapter adapter = new ParentAdapter(this, objectsLvl1);
            list.setAdapter(adapter);
        }
    

    这解决了我的问题,现在我有一个全屏ExpandableList

    如果有人告诉我如果我想通过我自己的CustomExpandableList 来做这件事,那将是很好的。因为我仍然不知道该怎么做。

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 2017-07-12
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      相关资源
      最近更新 更多