Scrollview中嵌套Expandablelistview

1.第一步:重写Expandablelistview


/**
 * 设置不能滑动的ExpandableListView
 * @author Administrator
 *
 */
public class CustomExpandableListView extends ExpandableListView {
    public CustomExpandableListView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        // TODO Auto-generated constructor stub  
    }  
 
    @Override  
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
        // TODO Auto-generated method stub  
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,  
 
        MeasureSpec.AT_MOST);  
 
        super.onMeasure(widthMeasureSpec, expandSpec);  
    }  
}


2.第二步:创建工具类设置Expandablelistview的高度

public class Utility {
     public static void setListViewHeight(ExpandableListView listView) {  
            ListAdapter listAdapter = listView.getAdapter();  
            int totalHeight = 0;  
//            int count = listAdapter.getCount();  
            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();  
        }

}

3.最后一步设置调用设置高度方法即可

Utility.setListViewHeight(expandableListView);

你可能感兴趣的:(小知识点)