我正在使用ScrollView中的可扩展ListView创建自定义可扩展视图。单击可展开的ListView时,它应该展开以覆盖屏幕的2/3。
我试过了:
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Utility.setListViewHeightBasedOnChildren(expListView);
return false;
}
});
Utility.java:
public class Utility {
public static int original_height;
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = (totalHeight + (listView.getDividerHeight() * (listAdapter
.getCount() - 1))) / 2;
listView.setLayoutParams(params);
listView.requestLayout();
}
如果放置
,相同的代码将起作用Utility.setListViewHeightBasedOnChildren(expListView);
设置列表适配器后此代码。