0
私のレイアウトにfragment
があり、フラグメントにはListView
が含まれています。フラグメントビューのサイズを元にしてフラグメントの高さを動的に増やす方法
今私はそうwrap_content
とmatch_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>
あなたの 'setListHeight'は気にしません...それはListViewからLinearLayoutを作る...また、' Adapter.getView'は、アダプタ内のすべての項目に対して少なくとも2回呼び出されます – Selvin