ExpandableListView是android中可以实现下拉list的一个控件,具体的实现方法如下:

首先:在layout的xml文件中定义一个ExpandableListView

< LinearLayout         android:id ="@+id/linearLayout"        android:layout_width ="fill_parent"         android:layout_height ="fill_parent"        androidrientation ="vertical"        >               < ExpandableListView        android:id ="@+id/expandableListView"        android:layout_width ="fill_parent"        android:layout_height ="wrap_content"            />    </ LinearLayout >  

定义两个List,用来存放控件中Group/Child中的String

private  List<String> groupArray;   private  List<List<String>> childArray;  

对这两个List进行初始化,并插入一些数据

groupArray = new  ArrayList<String>();   childArray = new  ArrayList<List<String>>();      groupArray.add("第一行" );   groupArray.add("第二行" );      List<String> tempArray = new  ArrayList<String>();   tempArray.add("第一条" );   tempArray.add("第二条" );   tempArray.add("第三条" );      for (int  index = 0 ; index <groupArray.size(); ++index)   {       childArray.add(tempArray);   }   

定义ExpandableListView的Adapter

//ExpandableListView的Adapter    public  class  ExpandableAdapter extends  BaseExpandableListAdapter   {       Activity activity;              public  ExpandableAdapter(Activity a)       {           activity = a;       }       public  Object getChild(int  groupPosition, int  childPosition)       {           return  childArray.get(groupPosition).get(childPosition);       }       public  long  getChildId(int  groupPosition, int  childPosition)       {           return  childPosition;       }       public  int  getChildrenCount(int  groupPosition)       {           return  childArray.get(groupPosition).size();       }       public  View getChildView(int  groupPosition, int  childPosition,               boolean  isLastChild, View convertView, ViewGroup parent)       {           String string = childArray.get(groupPosition).get(childPosition);           return  getGenericView(string);       }       // group method stub        public  Object getGroup(int  groupPosition)       {           return  groupArray.get(groupPosition);       }       public  int  getGroupCount()       {           return  groupArray.size();       }       public  long  getGroupId(int  groupPosition)       {           return  groupPosition;       }       public  View getGroupView(int  groupPosition, boolean  isExpanded,               View convertView, ViewGroup parent)       {           String string = groupArray.get(groupPosition);           return  getGenericView(string);       }       // View stub to create Group/Children 's View        public  TextView getGenericView(String string)       {           // Layout parameters for the ExpandableListView            AbsListView.LayoutParams layoutParams = new  AbsListView.LayoutParams(                   ViewGroup.LayoutParams.FILL_PARENT, 64 );           TextView text = new  TextView(activity);           text.setLayoutParams(layoutParams);           // Center the text vertically            text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);           // Set the text starting position            text.setPadding(36 , 0 , 0 , 0 );           text.setText(string);           return  text;       }       public  boolean  hasStableIds()       {           return  false ;       }       public  boolean  isChildSelectable(int  groupPosition, int  childPosition)       {           return  true ;       }   }   

最后,给定义好的ExpandableListView添加上Adapter

ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);   expandableListView.setAdapter(new  ExpandableAdapter(Main.this ));   

更多相关文章

  1. Android(安卓)VectorDrawable与SVG
  2. android 发送自定义广播以及接收自定义广播
  3. Android(安卓)在程序界面上显示图片
  4. android圆形进度条ProgressBar颜色设置
  5. 善用Android预定义样式
  6. TabHost+Fragment分析
  7. android设置view透明度的效果
  8. android中自定义进度条风格
  9. AndroidManifest.xml 中application 的 android:name 属性作用

随机推荐

  1. Android官方入门文档[13]暂停和恢复一个A
  2. Android(安卓)内存相关 onTrimMemory,onLo
  3. android ctl属性的实现过程
  4. Android中实现整个视图切换的左右滑动效
  5. android简单实现轮播图效果
  6. Android应用程序未捕获异常并提示用户发
  7. Android(安卓)自定义 Adapter
  8. Android(安卓)子线程修改UI方法对比
  9. Android中关于Audio库的知识
  10. 如何制作Jar包并在android中调用jar包