使用ExpandableListView

效果图


使用ExpandableListView_第1张图片

 

Group右边的图标是Android系统自动加上的默认图标

子列表默认不加任何图标

 

 

 

Res/layout/expandable_list_view.xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:orientation="vertical"  
  5.   android:layout_width="match_parent"  
  6.   android:layout_height="match_parent">  
  7.     <ExpandableListView android:id="@+id/expandable_list_view"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent" />  
  10. </LinearLayout>  

 

 

Java代码  收藏代码
  1. package com.improve;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.view.Gravity;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.AbsListView;  
  8. import android.widget.BaseExpandableListAdapter;  
  9. import android.widget.ExpandableListView;  
  10. import android.widget.ExpandableListView.OnChildClickListener;  
  11. import android.widget.ExpandableListView.OnGroupCollapseListener;  
  12. import android.widget.ExpandableListView.OnGroupExpandListener;  
  13. import android.widget.Toast;  
  14. import android.widget.ExpandableListView.OnGroupClickListener;  
  15. import android.widget.TextView;  
  16.   
  17. /** 
  18.  * ExpandableListView只能是两级层次 
  19.  * @author Davee 
  20.  */  
  21. public class ExpandableListViewDemo extends Activity {  
  22.     private List<String> groupData;  
  23.     private List<List<String>> childrenData;  
  24.     private void loadData() {  
  25.         groupData = new ArrayList<String>();  
  26.         groupData.add("Group 1");  
  27.         groupData.add("Group 2");  
  28.         groupData.add("Group 3");  
  29.   
  30.         childrenData = new ArrayList<List<String>>();  
  31.         List<String> sub1 = new ArrayList<String>();  
  32.         sub1.add("G1 Item 1");  
  33.         sub1.add("G1 Item 2");  
  34.         childrenData.add(sub1);  
  35.         List<String> sub2 = new ArrayList<String>();  
  36.         sub2.add("G2 Item 1");  
  37.         sub2.add("G2 Item 2");  
  38.         sub2.add("G2 Item 3");  
  39.         sub2.add("G2 Item 4");  
  40.         childrenData.add(sub2);  
  41.         List<String> sub3 = new ArrayList<String>();  
  42.         sub3.add("G3 Item 1");  
  43.         sub3.add("G3 Item 2");  
  44.         sub3.add("G3 Item 3");  
  45.         sub3.add("G3 Item 4");  
  46.         sub3.add("G3 Item 5");  
  47.         childrenData.add(sub3);  
  48.     }  
  49.   
  50.     @Override  
  51.     public void onCreate(Bundle savedInstanceState) {  
  52.         super.onCreate(savedInstanceState);  
  53.         setContentView(R.layout.expandable_list_view);  
  54.           
  55.         loadData();  
  56.           
  57.         ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandable_list_view);  
  58.         expandableListView.setAdapter(new ExpandableAdapter());  
  59.         expandableListView.setOnGroupClickListener(new OnGroupClickListener() {  
  60.             @Override  
  61.             public boolean onGroupClick(ExpandableListView parent, View clickedView, int groupPosition, long groupId) {  
  62.                 showMessage("点击Group: " + ((TextView)clickedView).getText());  
  63.                 return false;//返回true表示此事件在此被处理了  
  64.             }  
  65.         });  
  66.         expandableListView.setOnChildClickListener(new OnChildClickListener() {  
  67.             @Override  
  68.             public boolean onChildClick(ExpandableListView expandablelistview,  
  69.                     View clickedView, int groupPosition, int childPosition, long childId) {  
  70.                 showMessage("点击Child: " + ((TextView)clickedView).getText());  
  71.                 return false;//返回true表示此事件在此被处理了  
  72.             }  
  73.         });  
  74.         expandableListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {  
  75.             @Override  
  76.             public void onGroupCollapse(int groupPosition) {  
  77.                 showMessage("合拢Group: " + (groupPosition + 1));  
  78.             }  
  79.         });  
  80.         expandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {  
  81.             @Override  
  82.             public void onGroupExpand(int groupPosition) {  
  83.                 showMessage("展开Group: " + (groupPosition + 1));  
  84.             }  
  85.         });  
  86.     }  
  87.       
  88.     private class ExpandableAdapter extends BaseExpandableListAdapter {  
  89.   
  90.         @Override  
  91.         public Object getChild(int groupPosition, int childPosition) {  
  92.             return childrenData.get(groupPosition).get(childPosition);  
  93.         }  
  94.   
  95.         @Override  
  96.         public long getChildId(int groupPosition, int childPosition) {  
  97.             return 0;  
  98.         }  
  99.   
  100.         @Override  
  101.         public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {  
  102.             TextView text = null;  
  103.             if (convertView != null) {  
  104.                 text = (TextView)convertView;  
  105.                 text.setText(childrenData.get(groupPosition).get(childPosition));  
  106.             } else {  
  107.                 text = createView(childrenData.get(groupPosition).get(childPosition));  
  108.             }  
  109.             return text;  
  110.         }  
  111.   
  112.         @Override  
  113.         public int getChildrenCount(int groupPosition) {  
  114.             return childrenData.get(groupPosition).size();  
  115.         }  
  116.   
  117.         @Override  
  118.         public Object getGroup(int groupPosition) {  
  119.             return groupData.get(groupPosition);  
  120.         }  
  121.   
  122.         @Override  
  123.         public int getGroupCount() {  
  124.             return groupData.size();  
  125.         }  
  126.   
  127.         @Override  
  128.         public long getGroupId(int groupPosition) {  
  129.             return 0;  
  130.         }  
  131.   
  132.         @Override  
  133.         public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {  
  134.             TextView text = null;  
  135.             if (convertView != null) {  
  136.                 text = (TextView)convertView;  
  137.                 text.setText(groupData.get(groupPosition));  
  138.             } else {  
  139.                 text = createView(groupData.get(groupPosition));  
  140.             }  
  141.             return text;  
  142.         }  
  143.   
  144.         @Override  
  145.         public boolean hasStableIds() {  
  146.             return false;  
  147.         }  
  148.   
  149.         @Override  
  150.         public boolean isChildSelectable(int groupPosition, int childPosition) {  
  151.             return false;  
  152.         }  
  153.           
  154.         private TextView createView(String content) {  
  155.             AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(    
  156.                     ViewGroup.LayoutParams.FILL_PARENT, 38);    
  157.             TextView text = new TextView(ExpandableListViewDemo.this);    
  158.             text.setLayoutParams(layoutParams);    
  159.             text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);    
  160.             text.setPadding(40000);    
  161.             text.setText(content);  
  162.             return text;  
  163.         }  
  164.     }  
  165.       
  166.     private void showMessage(String message) {  
  167.         Toast.makeText(this, message, Toast.LENGTH_SHORT).show();  
  168.     }  
  169. }  

 

 

在上面效果图中,图标是系统自动加上的,也可以定义自己的图标

效果图


使用ExpandableListView_第2张图片

 

增加drawable文件

 

Res/drawable/group_icon_selector.xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_expanded="true" android:drawable="@drawable/narrow_expand" />  
  4.     <item android:drawable="@drawable/narrow_unexpand" />  
  5. </selector>  
 

修改布局

 

Res/layout/expandable_list_view.xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:orientation="vertical"  
  5.   android:layout_width="match_parent"  
  6.   android:layout_height="match_parent">  
  7.     <ExpandableListView android:id="@+id/expandable_list_view"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent"  
  10.         android:groupIndicator="@drawable/group_icon_selector" />  
  11. </LinearLayout>  
 

你可能感兴趣的:(使用ExpandableListView)