2013-04-10 69 views
0

我使用可扩展列表视图来制作3级别的层次结构,想知道如何设置内部列表的高度和宽度。 我知道我们有这个目的的测量,但在我的情况下,它不允许我捕获父列表视图的整个空间。如何衡量儿童可扩展列表的高度和宽度

可能是我给它错误的值,这里是我用来设置子可扩展列表的高度和宽度的代码。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
    { 

     widthMeasureSpec = MeasureSpec.makeMeasureSpec(960,MeasureSpec.AT_MOST); 
     heightMeasureSpec = MeasureSpec.makeMeasureSpec(800,MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    } 

目前,它表现为跟随

<ParentGroup1    > 
<ChildParentGroup> 
<Child1> 
<Child2> 
<child3> 
<ParentGroup2    > 

,它应该会出现如下图所示。

<ParentGroup1    > 
<ChildParentGroup   > 
<Child1      > 
<Child2      > 
<child3      > 
<ParentGroup2    > 

请告知/建议相同。

谢谢你的时间。

+0

你能解释你的可扩展列表视图使用任何截图。 – 2013-04-10 12:23:30

+0

嗨Venkata,但我没有那个回购上传图像堆栈溢出,但我可以给你一个我面临的问题的想法请参考更新的问题 – 2013-04-10 12:34:58

+0

是否所有的父母,所有的子父母和孩子有相同的布局设计? – 2013-04-10 12:36:52

回答

0

为ParentGroup和ChildParentGroup(Child的另一个布局xml文件)创建一个布局xml文件。现在你将问题简化为两个层次的层次结构。然后在Expandable listview中,我们有Parent视图和childview方法来膨胀并使用Parent和Child布局。所以在这种方法中,你可以做任何你想做的事情。

+0

对不起Venkata,但我的方法是类似的,你在答案中提到我正在接线输出。简而言之,我有一个可扩展的列表视图,它使用A-Adapter来填充getChildView上的数据我正在创建B-Expandable Listview并使用B-Adapter。现在的问题是当我删除B-Expandable的onmessaue方法时,B - Expandable列表使用A- Expandable列表的整个宽度,但B的子视图不可见。 – 2013-04-10 13:01:27

2

不知道你是否还在寻找答案,但我是这么做的:通过引用父视图和身高尺度(在这种情况下,我使用了子列表的大小)构造函数创建子自定义列表。

public CustomExpandableList(Context context, View the_parentView, int the_heightMeasure) 
{ 
    super(context); 
    WIDTH = the_parentView!=null?the_parentView.getWidth():LayoutParams.MATCH_PARENT; 
    HEIGHT = the_heightMeasure * 500; 
} 

编辑:或者使代码更一致,你可以在parentView的宽度和高度测量传递给构造函数,而不是通过自身的父视图。 CustomExpandableList(上下文the_context,INT the_width,INT the_heightMeasure)

+0

感谢您的宝贵意见。我想,我用其他方式解决了这个问题,也会应用这个,让我们知道。 – 2013-09-10 09:08:44

2

利用这个代码来计算动态扩展列表视图:

// calculate the height of expandable listView without expanded 
     private void setListViewHeight(ExpandableListView expListView) { 
    ListAdapter listAdapter = expListView.getAdapter(); 
    int totalHeight = 0; 

    for (int i = 0; i < listAdapter.getCount(); i++) { 
     View listItem = listAdapter.getView(i, null, expListView); 
     listItem.measure(0, 0); 
     totalHeight += listItem.getMeasuredHeight(); 
     System.out.println("i " + i); 
    } 

    ViewGroup.LayoutParams params = expListView.getLayoutParams(); 
    params.height = totalHeight 
      + (expListView.getDividerHeight() * (listAdapter.getCount() - 1)); 
    System.out.println("params.height = " + params.height); 

    expListView.setLayoutParams(params); 
    expListView.requestLayout(); 
} 

// calculate the height of expandable listview dynamically 
private void setListViewHeight(ExpandableListView expListView, int group) { 

    ExpandableListAdapter listAdapter = expListView 
      .getExpandableListAdapter(); 
    int totalHeight = 0; 
    int desiredWidth = MeasureSpec.makeMeasureSpec(expListView.getWidth(), 
      MeasureSpec.UNSPECIFIED); 
    for (int i = 0; i < listAdapter.getGroupCount(); i++) { 
     View groupItem = listAdapter.getGroupView(i, false, null, 
       expListView); 
     groupItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); 

     totalHeight += groupItem.getMeasuredHeight(); 

     if (((expListView.isGroupExpanded(i)) && (i == group)) 
       || ((!expListView.isGroupExpanded(i)) && (i == group))) { 

      for (int j = 0; j < listAdapter.getChildrenCount(i); j++) { 

       View listItem = listAdapter.getChildView(i, j, false, null, 
         expListView); 

       Log.e("Count", listAdapter.getChildrenCount(i) + ""); 

       listItem.setLayoutParams(new ViewGroup.LayoutParams(
         desiredWidth, MeasureSpec.UNSPECIFIED)); 
       // listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED); 
       listItem.measure(MeasureSpec.makeMeasureSpec(0, 
         MeasureSpec.UNSPECIFIED), MeasureSpec 
         .makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
       totalHeight += listItem.getMeasuredHeight(); 

       System.out.println("totalHeight" + totalHeight); 
       Log.e("TEST", "gshdkfmjfy,"); 

      } 
     } 
    } 

    ViewGroup.LayoutParams params = expListView.getLayoutParams(); 
    int height = totalHeight 
      + (expListView.getDividerHeight() * (listAdapter 
        .getGroupCount() - 1)); 

    if (height < 10) { 
     height = 100; 
    } 
    params.height = height; 
    expListView.setLayoutParams(params); 
    expListView.requestLayout(); 

} 
+0

我想知道,什么是INT组的值我私人无效setListViewHeight(ExpandableListView expListView,int组) – Vrajesh 2014-12-25 06:40:50

1

我成功地在一些天前这样做。它有点紧凑,没有任何额外的参数,并且完美地工作。

public static void setExpandableListViewHeightBasedOnChildren(ExpandableListView expandableListView){ 
    ExpandableNotesListAdapter adapter = (ExpandableNotesListAdapter) expandableListView.getExpandableListAdapter(); 
    if (adapter == null){ 
     return; 
    } 
    int totalHeight = expandableListView.getPaddingTop() + expandableListView.getPaddingBottom(); 
    for (int i = 0 ; i < adapter.getGroupCount() ; i++){ 
     View groupItem = adapter.getGroupView(i, false, null, expandableListView); 
     groupItem.measure(View.MeasureSpec.UNSPECIFIED,View.MeasureSpec.UNSPECIFIED); 
     totalHeight += groupItem.getMeasuredHeight(); 

     if (expandableListView.isGroupExpanded(i)){ 
      for(int j = 0 ; j < adapter.getChildrenCount(i) ; j++) { 
       View listItem = adapter.getChildView(i, j, false, null, expandableListView); 
       listItem.setLayoutParams(new LayoutParams(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)); 
       listItem.measure(View.MeasureSpec.makeMeasureSpec(0, 
         View.MeasureSpec.UNSPECIFIED), View.MeasureSpec 
         .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); 
       totalHeight += listItem.getMeasuredHeight(); 

      } 
     } 
    } 

    ViewGroup.LayoutParams params = expandableListView.getLayoutParams(); 
    int height = totalHeight + expandableListView.getDividerHeight() * (adapter.getGroupCount() - 1); 

    if (height < 10) 
     height = 100; 
    params.height = height; 
    expandableListView.setLayoutParams(params); 
    expandableListView.requestLayout(); 
} 

不要忘了,当你初始化你查看添加此,设置你的适配器等:

Functions.setExpandableListViewHeightBasedOnChildren(listView); 
listView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 
    @Override 
    public void onGroupExpand(int groupPosition) { 
     Functions.setExpandableListViewHeightBasedOnChildren(listView); 
    } 
}); 
0

只需拆除宽度代码,它应该工作的罚款。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
{ 
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST); 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 
0

我知道它晚了,但如果有人有同样的问题。您可以通过创建一个自定义ExpandableListView并使用“MeasureSpec.EXACTLY”解决这个问题:

public class CustomExpListView extends ExpandableListView{ 
    public CustomExpListView(Context context){ 
     super(context); 
    } 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ 
     widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.EXACTLY); 
     heightMeasureSpec = MeasureSpec.makeMeasureSpec(20000, MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    } 
} 

希望这有助于给任何人。对我来说它的工作。