ListView只显示第一个子项的情况下,需要设置它的布局参数,以显示全部子项。只需要调用以下函数即可。

privatevoidsetListViewHeightBasedOnChildren(ListViewlistView){
ListAdapterlistAdapter=listView.getAdapter();
if(listAdapter==null){
return;
}
inttotalHeight=0;
for(inti=0,len=listAdapter.getCount();i<len;i++){
ViewlistItem=listAdapter.getView(i,null,listView);
listItem.measure(0,0);
totalHeight+=listItem.getMeasuredHeight();
}
ViewGroup.LayoutParamsparams=listView.getLayoutParams();
params.height=totalHeight+(listView.getDividerHeight()*(listAdapter.getCount()-1));
listView.setLayoutParams(params);
}
privatevoidsetArrowHind(){
arrow.setVisibility(View.GONE);
}