Android 用ExpandableListView实现的文件目录树框架


主要练习ExpandableListView,文件的相关操作没有写,运行效果如图:


代码如下:

AppMain.java

  1. package lxy.litsoft;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import Android.app.Activity;  
  9. import android.os.Bundle;  
  10. import android.util.Log;  
  11. import android.view.View;  
  12. import android.widget.ExpandableListView;  
  13. import android.widget.ExpandableListView.OnChildClickListener;  
  14. import android.widget.SimpleExpandableListAdapter;  
  15.   
  16. public class AppMain extends Activity {  
  17.       
  18.     //声明对象   
  19.     private SimpleExpandableListAdapter adapter = null;  
  20.     List<Map<String,String>> groups;  
  21.     List<List<Map<String,String>>> childs;  
  22.     ExpandableListView expandableListView;  
  23.       
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.main);  
  27.           
  28.         //为ExpandableListView准备数据   
  29.         groups = new ArrayList<Map<String,String>>();  
  30.         Map<String,String> group1 = new HashMap<String,String>();  
  31.         group1.put("group","group1");  
  32.         Map<String,String> group2 = new HashMap<String,String>();  
  33.         group2.put("group","group2");  
  34.         groups.add(group1);  
  35.         groups.add(group2);  
  36.           
  37.         List<Map<String,String>> child1 = new ArrayList<Map<String,String>>();  
  38.         Map<String,String> child1Data1 = new HashMap<String,String>();  
  39.         child1Data1.put("child","child1Data1");  
  40.         child1.add(child1Data1);  
  41.         Map<String,String> child1Data2 = new HashMap<String,String>();  
  42.         child1Data2.put("child","child1Data2");  
  43.         child1.add(child1Data2);  
  44.           
  45.         List<Map<String,String>> child2 = new ArrayList<Map<String,String>>();  
  46.         Map<String,String> child1Data3 = new HashMap<String,String>();  
  47.         child1Data3.put("child","child1Data3");  
  48.         child2.add(child1Data3);  
  49.         Map<String,String> child1Data4 = new HashMap<String,String>();  
  50.         child1Data4.put("child","child1Data4");  
  51.         child2.add(child1Data4);  
  52.           
  53.         childs = new ArrayList<List<Map<String,String>>>();  
  54.         childs.add(child1);  
  55.         childs.add(child2);  
  56.           
  57.         //实例化ExpandableListView对象   
  58.         expandableListView = (ExpandableListView)findViewById(R.id.expandable);  
  59.         //实例化ExpandableListView的适配器   
  60.         adapter = new SimpleExpandableListAdapter(  
  61.                  this,  
  62.                  groups,  
  63.                  R.layout.group,  
  64.                  new String[] {"group"},  
  65.                  new int[]{ R.id.groupText},  
  66.                  childs,   
  67.                  R.layout.child,  
  68.                  new String[] { "child" },  
  69.                  new int[] { R.id.childText}  
  70.                 );  
  71.           
  72.         //设置适配器   
  73.         expandableListView.setAdapter(adapter);  
  74.         //设置监听器   
  75.         expandableListView.setOnChildClickListener(new OnChildClickListener() {  
  76.               
  77.             public boolean onChildClick(ExpandableListView parent, View v,  
  78.                     int groupPosition, int childPosition, long id) {  
  79.                 Log.d("test""GroupPosition is "+groupPosition);  
  80.                 Log.d("test","ChildPosition is"+childPosition);  
  81.                 return false;  
  82.             }  
  83.         });  
  84.     }  
  85. }  
main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/linearLayout1"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent" >  
  7.       
  8.     <LinearLayout  
  9.     android:orientation="horizontal"  
  10.     android:layout_weight="7"  
  11.     android:background="#FF555500"  
  12.     android:layout_width="fill_parent"  
  13.     android:layout_height="fill_parent" >  
  14.       
  15.         <TextView   
  16.         android:layout_width="fill_parent"   
  17.         android:layout_height="fill_parent"  
  18.         android:textColor="#000000"  
  19.         android:textSize="25pt"  
  20.         android:gravity="center_vertical|center_horizontal"  
  21.         android:text="Title...."/>  
  22.     </LinearLayout>  
  23.       
  24.       
  25.     <LinearLayout   
  26.     android:orientation="horizontal"  
  27.     android:layout_weight="1"  
  28.     android:layout_width="fill_parent"  
  29.     android:layout_height="fill_parent"  
  30.     >  
  31.         <LinearLayout   
  32.         android:orientation="vertical"  
  33.         android:layout_weight="3"  
  34.         android:background="#FF005500"  
  35.         android:layout_width="fill_parent"  
  36.         android:layout_height="fill_parent"  
  37.         >  
  38.             <!-- @id/android:id/list -->  
  39.             <ExpandableListView  
  40.                 android:id="@+id/expandable"  
  41.                 android:layout_height="fill_parent"  
  42.                 android:layout_width="fill_parent"  
  43.                 android:drawSelectorOnTop="false"></ExpandableListView>  
  44.                   
  45.             <TextView android:id="@id/android:empty"  
  46.                 android:layout_width="fill_parent"   
  47.                 android:layout_height="fill_parent"  
  48.                 android:text="No data"/>  
  49.         </LinearLayout>  
  50.           
  51.         <LinearLayout   
  52.         android:orientation="horizontal"  
  53.         android:layout_weight="1"  
  54.         android:background="#FFFFFFFF"  
  55.         android:layout_width="fill_parent"  
  56.         android:layout_height="fill_parent"  
  57.         >  
  58.             <TextView   
  59.             android:layout_width="fill_parent"   
  60.             android:layout_height="fill_parent"  
  61.             android:textColor="#000000"  
  62.             android:textSize="25pt"  
  63.             android:gravity="center_vertical|center_horizontal"  
  64.             android:text="Content"/>  
  65.         </LinearLayout>  
  66.     </LinearLayout>  
  67. </LinearLayout>  
group.xml
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  3.      android:layout_width="fill_parent"   
  4.      android:layout_height="wrap_content"   
  5.      android:orientation="vertical">    
  6.     <TextView      
  7.         xmlns:android="http://schemas.android.com/apk/res/android"    
  8.         android:id="@+id/groupText"  
  9.         android:layout_marginLeft="40dip"  
  10.         android:textSize="15pt"  
  11.         android:textStyle="bold"  
  12.         android:textColor="#000000"  
  13.         android:layout_width="fill_parent"     
  14.         android:layout_height="wrap_content"  
  15.         android:text="NoData" />    
  16. </LinearLayout>  
child.xml
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  3.      android:layout_width="fill_parent"   
  4.      android:layout_height="wrap_content"   
  5.      android:orientation="vertical">    
  6.     <TextView      
  7.         android:id="@+id/childText"   
  8.         android:layout_marginLeft="60dip"  
  9.         android:textSize="10pt"  
  10.         android:textColor="#000000"  
  11.         android:layout_width="fill_parent"     
  12.         android:layout_height="wrap_content"   
  13.         android:text="CNoData" />   
  14. </LinearLayout>   

相关内容