listview嵌套listview子listview显示不全的问题

前辈们给出了一个方法,重新计算子ListView的大小,然后在设置本ListView的Adapter之后运行这个方法就好了,具体代码如下:

/**
 * 设置Listview的高度
 */
public void setListViewHeight(ListView listView) {
    ListAdapter listAdapter = 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);
}

这个方法设置的item的Layout必须是带有onMeasure()方法的控件,否则在计算的时候会报错,建议使用LinearLayout。

下面是我父listview的adpter的getview代码

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = mInflater.inflate(
                R.layout.privatecustomer_item, parent, false);
        holder.bt1 = (Button) convertView.findViewById(R.id.bt_jiatingxinxi);
        holder.lv1 = (ListView) convertView.findViewById(R.id.lv_item_in_privatecustomer);
        holder.tv1 = (TextView) convertView.findViewById(R.id.tv1_main_tab03_item);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.lv1.setAdapter(madpterson);
    setListViewHeight(holder.lv1);//子listview设置了adapter之后,再设置高度,就ok
    return convertView;
}

你可能感兴趣的:(Android)