2016-10-10 9 views
0

私のレイアウトにfragmentがあり、フラグメントにはListViewが含まれています。フラグメントビューのサイズを元にしてフラグメントの高さを動的に増やす方法

今私はそうwrap_contentmatch_parentはそのheightに影響を与えていない、私は動的に私のListViewの高さを増やすには、次のappracohを使用し、動的に私のListViewにデータを追加このメソッドはListViewで完全に機能しますが、ここでは失敗します。

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です。私はListView高さがFragment高さに影響を与えるされていないため、動的にFragmentの高さを高くする方法を理解することができませんし、それはまだfragment-android-listview-exampleチュートリアルを確認してください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'は、アダプタ内のすべての項目に対して少なくとも2回呼び出されます – Selvin

答えて

1

にのみ1つのアイテムを示します。

関連する問題