2016-10-10 43 views
0

我的佈局中有fragment,片段中包含ListView如何根據列表視圖的大小動態增加片段高度

現在我將數據添加到我的ListView動態,所以wrap_contentmatch_parent不影響其height,我用以下appracoh動態增加我的ListView的高度。此方法在ListViews上完美工作,但在此失敗。

public void setListHeight(ListView listView) { 
     DataListAdapter listAdapter= (DataListAdapter)listView.getAdapter(); 
     if (listAdapter == null) { 
      return; 
     } 
     int totalHeight = 0; 
     for (int i = 0; i < listAdapter.getCount(); i++) { 
      View listItem = listAdapter.getView(i, null, listView); 
      listItem.measure(0, 0); 
      totalHeight += listItem.getMeasuredHeight(); 
     } 
     ViewGroup.LayoutParams params = listView.getLayoutParams(); 
     params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); 
     listView.setLayoutParams(params); 
     listView.requestLayout(); 
    } 

我可以動態地通過使用上述方法增加我的ListView的高度,但片段的高度仍wrap_content。我無法理解如何增加Fragment動態的高度,因爲ListView高度不影響Fragment高度,它仍然顯示ListView

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <fragment 
       class="com.myproject.fragments.fragmentControl" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/fragmentId"/> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="1dp" 
       android:background="@android:color/white"/> 

     </LinearLayout> 
+0

你的'setListHeight'沒有做出靈敏...它使得來自ListView的LinearLayout ...同時'Adapter.getView'對於適配器中的每個項目都被調用至少兩次 – Selvin

回答

相關問題