主要练习ExpandableListView,文件的相关操作没有写,运行效果如图:
代码如下:
AppMain.java
package lxy.litsoft; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.SimpleExpandableListAdapter; public class AppMain extends Activity { //声明对象 private SimpleExpandableListAdapter adapter = null; List<Map<String,String>> groups; List<List<Map<String,String>>> childs; ExpandableListView expandableListView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //为ExpandableListView准备数据 groups = new ArrayList<Map<String,String>>(); Map<String,String> group1 = new HashMap<String,String>(); group1.put("group","group1"); Map<String,String> group2 = new HashMap<String,String>(); group2.put("group","group2"); groups.add(group1); groups.add(group2); List<Map<String,String>> child1 = new ArrayList<Map<String,String>>(); Map<String,String> child1Data1 = new HashMap<String,String>(); child1Data1.put("child","child1Data1"); child1.add(child1Data1); Map<String,String> child1Data2 = new HashMap<String,String>(); child1Data2.put("child","child1Data2"); child1.add(child1Data2); List<Map<String,String>> child2 = new ArrayList<Map<String,String>>(); Map<String,String> child1Data3 = new HashMap<String,String>(); child1Data3.put("child","child1Data3"); child2.add(child1Data3); Map<String,String> child1Data4 = new HashMap<String,String>(); child1Data4.put("child","child1Data4"); child2.add(child1Data4); childs = new ArrayList<List<Map<String,String>>>(); childs.add(child1); childs.add(child2); //实例化ExpandableListView对象 expandableListView = (ExpandableListView)findViewById(R.id.expandable); //实例化ExpandableListView的适配器 adapter = new SimpleExpandableListAdapter( this, groups, R.layout.group, new String[] {"group"}, new int[]{ R.id.groupText}, childs, R.layout.child, new String[] { "child" }, new int[] { R.id.childText} ); //设置适配器 expandableListView.setAdapter(adapter); //设置监听器 expandableListView.setOnChildClickListener(new OnChildClickListener() { public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Log.d("test", "GroupPosition is "+groupPosition); Log.d("test","ChildPosition is"+childPosition); return false; } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="horizontal" android:layout_weight="7" android:background="#FF555500" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="#000000" android:textSize="25pt" android:gravity="center_vertical|center_horizontal" android:text="Title...."/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="vertical" android:layout_weight="3" android:background="#FF005500" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- @id/android:id/list --> <ExpandableListView android:id="@+id/expandable" android:layout_height="fill_parent" android:layout_width="fill_parent" android:drawSelectorOnTop="false"></ExpandableListView> <TextView android:id="@id/android:empty" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="No data"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_weight="1" android:background="#FFFFFFFF" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="#000000" android:textSize="25pt" android:gravity="center_vertical|center_horizontal" android:text="Content"/> </LinearLayout> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/groupText" android:layout_marginLeft="40dip" android:textSize="15pt" android:textStyle="bold" android:textColor="#000000" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="NoData" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/childText" android:layout_marginLeft="60dip" android:textSize="10pt" android:textColor="#000000" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="CNoData" /> </LinearLayout>