另一个ExpandableListView问题中的ExpandableListView

时间:2015-11-03 14:52:57

标签: android listview scrollview expandablelistview

我正在尝试创建2级expandableListView。我可以获取数据并完美地绑定到UI元素,但两个子节点之间的高度是固定的。 因为内部的ExpandableListView就像在scrollview中,我只看到一个项目。

*group1
 ^child1
   //data
 ^child2
   //data
*group2
 ^child1
  //data
^child2
 //dataa

我正在动态设置孩子的身高。即使用以下方法在group1和group2之间设置高度:

ListAdapter listadp = lv_ancillaryservice.getAdapter();
       if (listadp != null) {
           int totalHeight = 0;
           for (int i = 0; i < listadp.getCount(); i++) {
               View listItem = listadp.getView(i, null, lv_ancillaryservice);
               listItem.measure(0, 0);
               totalHeight += listItem.getMeasuredHeight();
           }
           ViewGroup.LayoutParams params = lv_ancillaryservice.getLayoutParams();
           params.height = totalHeight + (lv_ancillaryservice.getDividerHeight() * (listadp.getCount() - 1));
           lv_ancillaryservice.setLayoutParams(params);
           lv_ancillaryservice.requestLayout();

我之前使用过,因为usingwrap_content对listview不起作用。现在的问题是:

  1. 当点击group1时,显示^ child1和^ child2,当点击child1时,我有一个listview但我无法看到完整列表。(group2位置是固定的)
  2. 滚动是没有工作的地方。

1 个答案:

答案 0 :(得分:0)

我使用此链接https://github.com/PaoloRotolo/ExpandableHeightListView解决了这个问题。

package com.rtt.reeferwatch.utilities;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

public class ExpandableHeightListView extends ExpandableListView {

boolean expanded = false;

public ExpandableHeightListView(Context context) {
    super(context);
}

public ExpandableHeightListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public ExpandableHeightListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public boolean isExpanded() {
    return expanded;
}


@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (isExpanded()) {
        int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    } else {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

public void setExpanded(boolean expanded) {
    this.expanded = expanded;
}
}