android scrollview中嵌套expandablelistview高度问题

来源:互联网 发布:美微香蕉网络电视 编辑:程序博客网 时间:2024/05/10 22:32

本文转载至http://blog.csdn.net/swust_chenpeng/article/details/17413955

一、重新expandablelistview

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class CustomExpandableListView extends ExpandableListView {  
  2.   
  3.     public CustomExpandableListView(Context context, AttributeSet attrs) {  
  4.         super(context, attrs);  
  5.         // TODO Auto-generated constructor stub  
  6.     }  
  7.   
  8.     @Override  
  9.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  10.         // TODO Auto-generated method stub  
  11.         int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,  
  12.   
  13.         MeasureSpec.AT_MOST);  
  14.   
  15.         super.onMeasure(widthMeasureSpec, expandSpec);  
  16.     }  
  17. }  

二、动态计算expandablelistview的高度,xxx_group.xml和xxx_child.xml的最外层要用linearlayout,反正relativelayout不行,不知道为什么

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. private void setListViewHeight(ExpandableListView listView) {  
  2.         ListAdapter listAdapter = listView.getAdapter();  
  3.         int totalHeight = 0;  
  4.         int count = listAdapter.getCount();  
  5.         for (int i = 0; i < listAdapter.getCount(); i++) {  
  6.             View listItem = listAdapter.getView(i, null, listView);  
  7.             listItem.measure(00);  
  8.             totalHeight += listItem.getMeasuredHeight();  
  9.         }  
  10.   
  11.         ViewGroup.LayoutParams params = listView.getLayoutParams();  
  12.         params.height = totalHeight  
  13.                 + (listView.getDividerHeight() * (listAdapter.getCount() - 1));  
  14.         listView.setLayoutParams(params);  
  15.         listView.requestLayout();  
  16.     }  

三、scrollveiew中嵌套的listview的话,前两种都行,还有一种如下

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class ViewGroupForListView extends LinearLayout implements View.OnClickListener {  
  2.       
  3.     private ListAdapter mAdapter = null;  
  4.     private OnItemClickListener mListener = null;  
  5.       
  6.     public ViewGroupForListView(Context context) {  
  7.         super(context);  
  8.     }  
  9.   
  10.     public ViewGroupForListView(Context context, AttributeSet attrs) {  
  11.         super(context, attrs);  
  12.         // TODO Auto-generated constructor stub  
  13.         this.setOrientation(VERTICAL);  
  14.     }  
  15.       
  16.     /** 
  17.      * 绑定数据 
  18.      */  
  19.     protected void bindData() {  
  20.         int count = mAdapter.getCount();  
  21.         for(int i = 0; i < count; i++) {  
  22.             View v = mAdapter.getView(i, nullnull);  
  23.             v.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  
  24.             v.setOnClickListener(this);  
  25.             v.setId(i);  
  26.             addView(v, i);  
  27.         }  
  28.     }  
  29.       
  30.     /** 
  31.      * 设置adapter 
  32.      * @param adapter 
  33.      */  
  34.     public void setAdapter(ListAdapter adapter) {  
  35.         mAdapter = adapter;  
  36.         if(this.getChildCount() != 0) {  
  37.             removeAllViews();  
  38.         }  
  39.         bindData();  
  40.     }  
  41.       
  42.     /** 
  43.      * 获取adapter 
  44.      * @return 
  45.      */  
  46.     public ListAdapter getAdapter() {  
  47.         return mAdapter;  
  48.     }  
  49.       
  50.     /** 
  51.      * 绑定监听 
  52.      * @param listener 
  53.      */  
  54.     public void setOnItemClickListener(OnItemClickListener listener) {  
  55.         this.mListener = listener;  
  56.     }  
  57.       
  58.     @Override  
  59.     public void onClick(View v) {  
  60.         // TODO Auto-generated method stub  
  61.         if(mListener != null) {  
  62.             mListener.onItemClick(v.getId(), mAdapter);  
  63.         }  
  64.     }  
  65.       
  66.     /** 
  67.      * 监听接口 
  68.      * @author Visual 
  69.      * 
  70.      */  
  71.     public interface OnItemClickListener {  
  72.         public void onItemClick(int position, ListAdapter adapter);  
  73.     }  
  74.       

adapter的实现类似普通listview的adapter用法
0 0