http://blog.csdn.net/swust_chenpeng/article/details/17413955

一、重新expandablelistview

[java] view plain copy
  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 plain copy
  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 plain copy
  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用法

更多相关文章

  1. Android 解决 RecyclerView 嵌套 ScrollView 数据显示不全的问题
  2. Android Studio App LinearLayout多层布局嵌套
  3. Android - ScrollView 使用小计 里面嵌套的View 如何设置全屏
  4. Java乔晓松-android的四大组件之一Service(服务的绑定)
  5. Android 架构组件之 ViewBinding(视图绑定)
  6. Android中ScrollView嵌套ListView只显示一行的解决方案
  7. Android中ScrollView中嵌套RecyclerView的完美解决办法
  8. Android NestedScrolling机制完全解析 带你玩转嵌套滑动
  9. Android数据绑定Data Binding初体验

随机推荐

  1. 搞懂java中的synchronized关键字
  2. SpringBoot结合MyBatis Plus 自动生成代
  3. java之concurrent包中的Atomic类
  4. SpringBoot项目通用功能及框架知识点介绍
  5. 深入理解JVM—JVM内存模型
  6. mymes部署虚拟机安装以及使用Linux,看一
  7. 学习Linux的第7天
  8. SpringBoot实现POI报表操作
  9. 深入浅出Java并发包—CountDownLauch原理
  10. SpringBoot结合POI百万级数据报表操作