2013-04-10 66 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); 
    } 
} 

希望這有助於給任何人。對我來說它的工作。