如何根据列表视图的大小动态增加片段高度
问题描述:
我的布局中有fragment
,片段中包含ListView
。如何根据列表视图的大小动态增加片段高度
现在我将数据添加到我的ListView
动态,所以wrap_content
和match_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>
你的'setListHeight'没有做出灵敏...它使得来自ListView的LinearLayout ...同时'Adapter.getView'对于适配器中的每个项目都被调用至少两次 – Selvin