Android中ExpandableListView的使用

来源:互联网 发布:java大数据是什么 编辑:程序博客网 时间:2024/05/21 05:37

Android中ExpandableListView的使用

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

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

view plain copy to clipboard print ?
  1. < LinearLayout    
  2.     android:id ="@+id/linearLayout"   
  3.     android:layout_width ="fill_parent"    
  4.     android:layout_height ="fill_parent"   
  5.     androidrientation ="vertical"   
  6.     >   
  7.       
  8.     < ExpandableListView   
  9.     android:id ="@+id/expandableListView"   
  10.     android:layout_width ="fill_parent"   
  11.     android:layout_height ="wrap_content"   
  12.         />   
  13. </ LinearLayout >   
 

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

view plain copy to clipboard print ?
  1. private  List<String> groupArray;  
  2. private  List<List<String>> childArray;  
 

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

view plain copy to clipboard print ?
  1. groupArray = new  ArrayList<String>();  
  2. childArray = new  ArrayList<List<String>>();  
  3.   
  4. groupArray.add("第一行" );  
  5. groupArray.add("第二行" );  
  6.   
  7. List<String> tempArray = new  ArrayList<String>();  
  8. tempArray.add("第一条" );  
  9. tempArray.add("第二条" );  
  10. tempArray.add("第三条" );  
  11.   
  12. for (int  index = 0 ; index <groupArray.size(); ++index)  
  13. {  
  14.     childArray.add(tempArray);  
  15. }  

 

定义ExpandableListView的Adapter

view plain copy to clipboard print ?
  1. //ExpandableListView的Adapter   
  2. public  class  ExpandableAdapter extends  BaseExpandableListAdapter  
  3. {  
  4.     Activity activity;  
  5.       
  6.     public  ExpandableAdapter(Activity a)  
  7.     {  
  8.         activity = a;  
  9.     }  
  10.     public  Object getChild(int  groupPosition, int  childPosition)  
  11.     {  
  12.         return  childArray.get(groupPosition).get(childPosition);  
  13.     }  
  14.     public  long  getChildId(int  groupPosition, int  childPosition)  
  15.     {  
  16.         return  childPosition;  
  17.     }  
  18.     public  int  getChildrenCount(int  groupPosition)  
  19.     {  
  20.         return  childArray.get(groupPosition).size();  
  21.     }  
  22.     public  View getChildView(int  groupPosition, int  childPosition,  
  23.             boolean  isLastChild, View convertView, ViewGroup parent)  
  24.     {  
  25.         String string = childArray.get(groupPosition).get(childPosition);  
  26.         return  getGenericView(string);  
  27.     }  
  28.     // group method stub   
  29.     public  Object getGroup(int  groupPosition)  
  30.     {  
  31.         return  groupArray.get(groupPosition);  
  32.     }  
  33.     public  int  getGroupCount()  
  34.     {  
  35.         return  groupArray.size();  
  36.     }  
  37.     public  long  getGroupId(int  groupPosition)  
  38.     {  
  39.         return  groupPosition;  
  40.     }  
  41.     public  View getGroupView(int  groupPosition, boolean  isExpanded,  
  42.             View convertView, ViewGroup parent)  
  43.     {  
  44.         String string = groupArray.get(groupPosition);  
  45.         return  getGenericView(string);  
  46.     }  
  47.     // View stub to create Group/Children 's View   
  48.     public  TextView getGenericView(String string)  
  49.     {  
  50.         // Layout parameters for the ExpandableListView   
  51.         AbsListView.LayoutParams layoutParams = new  AbsListView.LayoutParams(  
  52.                 ViewGroup.LayoutParams.FILL_PARENT, 64 );  
  53.         TextView text = new  TextView(activity);  
  54.         text.setLayoutParams(layoutParams);  
  55.         // Center the text vertically   
  56.         text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
  57.         // Set the text starting position   
  58.         text.setPadding(36 , 0 , 0 , 0 );  
  59.         text.setText(string);  
  60.         return  text;  
  61.     }  
  62.     public  boolean  hasStableIds()  
  63.     {  
  64.         return  false ;  
  65.     }  
  66.     public  boolean  isChildSelectable(int  groupPosition, int  childPosition)  
  67.     {  
  68.         return  true ;  
  69.     }  
  70. }  

 

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

view plain copy to clipboard print ?
  1. ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);  
  2. expandableListView.setAdapter(new  ExpandableAdapter(Main.this ));  

 

运行即可见效果~~~

 

----------------------------------------------------------------------------------------------------------------

Android版手风琴(ExpandableListView)

先看效果,过瘾一番。

  

 下载地址:http://download.csdn.net/detail/a358763471/7755137

0 0
原创粉丝点击