——————————————————————————————————————————————转载哦
下面通过我自己的学习来总结一下,ExpandableListActivity和SimpleExpandableListAdapter的使用。当我们想要给客户展示一组数据(这里我们之前是使用ListActivity),但数据里面又进行了分组,这就不能使用ListActivity,就要用我们下面要讲的ExpandableListActivity。
那请问:ExpandableListActivity和SimpleExpandableListAdapter两者有什么关系了?这就好像ListActivity和SimpleListAdapter一样,SimpleExpandableListAdapter为ExpandableListActivity提供数据。好的,下面就来讲解ExpandableListActivity和SimpleExpandableListAdapte的使用步骤:
1.在布局文件当中声明ExpandableActivity控件:
Main.xml:
<ExpandableListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
<TextView
android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Nodata"
/>
2.在layout文件夹当中,新建group.xml,为一级条目声明样式
Gruop.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/groupto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="10px"
android:paddingLeft="60px"
android:paddingBottom="10px"
android:textSize="26sp"
android:text="No data"
/>
</LinearLayout>
3.在layout文件夹当中,新建child.xml,为二级条目声明样式
Child.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/childto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="5px"
android:paddingLeft="50px"
android:paddingBottom="5px"
android:textSize="20sp"
android:text="No data"
/>
</LinearLayout>
4.创建一个Activity,继承ExpandableListActivity:
代码具体如下:
MainActivity.java:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
importandroid.app.ExpandableListActivity;
import android.os.Bundle;
importandroid.widget.SimpleExpandableListAdapter;
/*
* 创建一个Activity,继承ExpandableListAcitivty
*/
public class MainActivityextends ExpandableListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//定义一个List,该List对象为一级条目提供数据
List<Map<String,String>> 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,该List对象为第一个一级条目提供二级条目的数据
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,该List对象为第二个一级条目提供二级条目的数据
List<Map<String,String>> child2 = new ArrayList<Map<String, String>>();
Map<String,String> child2Data = new HashMap<String, String>();
child2Data.put("child","child2Data");
child2.add(child2Data);
//定义一个List,该List对象用来存储所有的二级条目的数据
List<List<Map<String,String>>> childs = new ArrayList<List<Map<String,String>>>();
childs.add(child1);
childs.add(child2);
//生成一个SimpleExpandableListAdapter对象
//1.context
//2.一级条目的数据
//3.用来设置一级条目样式的布局文件
//4.指定一级条目数据的key
//5.指定一级条目数据显示控件的id
//6.指定二级条目的数据
//7.用来设置二级条目样式的布局文件
//8.指定二级条目数据的key
//9.指定二级条目数据显示控件的id
SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
this, groups,R.layout.group, new String[] { "group" },
new int[] { R.id.groupTo}, childs, R.layout.child,
new String[] {"child" }, new int[] { R.id.childTo });
//将SimpleExpandableListAdapter对象设置给当前的ExpandableListActivity
setListAdapter(sela);
}
}
——————————————————————————————————————————————转载哦
public class
SimpleExpandableListAdapter
extends BaseExpandableListAdapter
java.lang.Object
android.widget.BaseExpandableListAdapter
android.widget.SimpleExpandableListAdapter
Class Overview
An easy adapter to map static data to group and child views defined in an XML file.
You can separately specify the data backing the group as a List of Maps.
Each entry in the ArrayList corresponds to one group in the expandable list.
The Maps contain the data for each row. You also specify an XML file that defines the views used to display a group,
and a mapping from keys in the Map to specific views. This process is similar for a child,
except it is one-level deeper so the data backing is specified as a List>,
where the first List corresponds to the group of the child,
the second List corresponds to the position of the child within the group,
and finally the Map holds the data for that particular child.
SimpleExpandableListAdapter继承于BaseExpandableListAdapter。
他负责把静态的group数据和child数据映射到XML文件中定的group和child视图上。
SimpleExpandableListAdapter无子类。
SimpleExpandableListAdapter的构造函数如下
SimpleExpandableListAdapter(Context context, List<? extends Map<String, ?>> groupData, int groupLayout,
String[] groupFrom, int[] groupTo,
List<? extends List<? extends Map<String, ?>>> childData, int childLayout, String[] childFrom, int[] childTo)
参数:
1,context:上下文。
2,groupData:group数据,它是基于Map的list。Data里边的每一项都和 ListView里边的每一项对应。
groupData里边的每一项都是一个Map类型,这个Map类里边包含了ListView每一行需要的数据。
3,groupLayout :就是group布局文件id,可引用系统提供的,也可以自定义。
4,groupFrom:这是个名字数组,每个名字是为了group在 ArrayList数组的每一个item索引Map<String,Object>的Object用的。
5,groupTo:里面是一个为group显示用的TextView数组。这些 TextView是以id的形式来表示的。例如:Android.R.id.text1,这个text1在layout当中是可以索引的。
6,childData:child数据,它是基于Map的list。Data里边的每一项都和 ListView里边的每一项对应。
childData里边的每一项都是一个Map类型,这个Map类里边包含了ListView每一行需要的数据。
3,childLayout :就是child的布局文件id,可引用系统提供的,也可以自定义。
4,childFrom:这是个名字数组,每个名字是为了child在 ArrayList数组的每一个item索引Map<String,Object>的Object用的。
5,childTo:里面是一个为child显示用的TextView数组。这些 TextView是以id的形式来表示的。例如:Android.R.id.text1,这个text1在layout当中是可以索引的。
SimpleExpandableListAdapter(Context context, List<? extends Map<String, ?>> groupData, int expandedGroupLayout, int collapsedGroupLayout,
String[] groupFrom, int[] groupTo,
List<? extends List<? extends Map<String, ?>>> childData, int childLayout,
String[] childFrom, int[] childTo)
SimpleExpandableListAdapter(Context context, List<? extends Map<String, ?>> groupData, int expandedGroupLayout, int collapsedGroupLayout,
String[] groupFrom, int[] groupTo,
List<? extends List<? extends Map<String, ?>>> childData, int childLayout, int lastChildLayout,
String[] childFrom, int[] childTo)
主要SimpleExpandableListAdapter提供了以下两个函数(增加的)
View newChildView(boolean isLastChild, ViewGroup parent)
Instantiates a new View for a child.
View newGroupView(boolean isExpanded, ViewGroup parent)
Instantiates a new View for a group.
更多可参考《BaseExpandableListAdapter》和《SimpleAdapter》
实例1:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleExpandableListAdapter;
import com.sec.android.touchwiz.samples.R;
public class ExpandableListSample4Xml extends ExpandableListActivity {
private int mGroupIdColumnIndex;
// Sample data set.
private String mMailboxList[]={
"Inbox", "Sentbox", "Draft",
"Outbox", "Email inbox", "Conversation",
"Trash", "My folders"
};
private String mSender[][] = { {"Andrew1", "Andrew2", "Andrew3", "Andrew4","Andrew5", "Andrew6"},
{"Jeff1", "Jeff2", "Jeff3", "Jeff4", "Jeff5", "Jeff6", "Jeff7"},
{"ED1", "ED2", "ED3", "ED4", "ED5"},
{"Rachel1", "Rachel2", "Rachel3", "Rachel4"},
{"Sean1", "Sean2", "Sean3", "Sean4", "Sean5"},
{"Karen1", "Karen2", "Karen3", "Karen4", "Karen5", "Karen6", "Karen7"},
{"Lisa1", "Lisa2", "Lisa3", "Lisa4", "Lisa5"},
{"Rain1", "Rain2", "Rain3", "Rain4", "Rain5"}};
private String mMailContext[][] = { {"I'm going to go to watch movie!!!!!!!!!!!!!!!!!!!!!!!!!!", "Hello....", "Nice to meet you", "Call me plz..", "Are you busy", "Call me plz.." },
{"hi" , "Call me plz.." , "hello", "I'm going to go to watch movie!!!!!!!!!!!!!!!!!!!!!!!!!!", "Hello....", "Nice to meet you", "Call me plz.."},
{"hi,baby", "Call me plz.." , "hi" , "Call me plz.." , "hello"},
{"I'm going to go to watch movie!!!!!!!!!!!!!!!!!!!!!!!!!!", "Hello....", "Nice to meet you", "Call me plz.."},
{"hi", "Call me plz.." , "hi" , "Call me plz.." , "?????"},
{"12312412","12414124","12412421","458645754","dfgsdgsdf","sdfgdfgh","sdfgdsfgh"},
{"vclkj","lkvoajh","alskdjfoivj","jvlkjasldkf","79837214"},
{"975432","kbvhuoisj","4362435,","342532","236dgasdt"}};
private String groupFrom[] = {"title"};
private int groupTo[] = {android.R.id.text1};
private String childFrom[] = {"name", "message"};
private int childTo[] = {R.id.child_name, R.id.child_message};
private ExpandableListAdapter mAdapter;
private ArrayList<Map<String, String>> groupData =
new ArrayList<Map<String, String>>();
private ArrayList<ArrayList<Map<String, String>>> childData =
new ArrayList<ArrayList<Map<String, String>>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Query for people
Cursor groupCursor = managedQuery(Phone.CONTENT_URI,
new String[] {Phone._ID, Phone.DISPLAY_NAME}, null, null, null);
// Cache the ID column index
mGroupIdColumnIndex = groupCursor.getColumnIndexOrThrow(Phone._ID);
// make data for group & child
for(int i = 0; i < mMailboxList.length; i++) {
HashMap<String, String> tempGroupData = new HashMap<String, String>();
ArrayList<Map<String, String>> tmpChild = new ArrayList<Map<String, String>>();
tempGroupData.put(groupFrom[0], mMailboxList<wbr>);<br> for(int j = 0; j < mSender<em><wbr>.length; j++) {<br> HashMap<String, String> tempChildData = new HashMap<String, String>();<br> tempChildData.put(childFrom[0], mSender<em><wbr>[j]);<br> tempChildData.put(childFrom[1], mMailContext<em><wbr>[j]);<br><br> tmpChild.add(tempChildData);<br> }<br> groupData.add(tempGroupData);<br> childData.add(tmpChild);<br> }<br> // Set up our adapter<br> mAdapter = new MyExpandableListAdapter(this,<br> groupData,<br> android.R.layout.simple_expandable_list_item_1,<br> groupFrom,<br> groupTo,<br> childData,<br> R.layout.expandablelistsample4xml_child,<br> childFrom,<br> childTo);<br> setListAdapter(mAdapter);<br> }<br> public class MyExpandableListAdapter extends SimpleExpandableListAdapter {<br> public MyExpandableListAdapter(Context context,<br> List<? extends Map<String, ?>> groupData, <br> int groupLayout,<br> String[] groupFrom, <br> int[] groupTo,<br> List<? extends List<? extends Map<String, ?>>> childData,<br> int childLayout, <br> String[] childFrom, <br> int[] childTo) {<br> super(context, groupData, groupLayout, groupFrom, groupTo, childData,<br> childLayout, childFrom, childTo);<br> }<br> }<br> }</wbr></em></wbr></em></wbr></em></wbr>
——————————————————————————————————————————————转载哦
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleExpandableListAdapter;
public class ExpandableList1 extends ExpandableListActivity
{
private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";
private ExpandableListAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < 20; i++)
{
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(NAME, "Group " + i);
curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even"
: "This group is odd");
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for (int j = 0; j < 15; j++)
{
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(NAME, "Child " + j);
curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even"
: "This child is odd");
}
childData.add(children);
}
// Set up our adapter
mAdapter = new SimpleExpandableListAdapter(this, groupData, // 存储父list的数据
android.R.layout.simple_expandable_list_item_2, // 父list的现实方式
new String[]
{
NAME, IS_EVEN
}, // 父list需要显示的数据
new int[]
{
android.R.id.text1, android.R.id.text2
}, // 父list的数据绑定到的view
childData, // 子list的数据
android.R.layout.simple_expandable_list_item_2, new String[]
{
NAME, IS_EVEN
}, new int[]
{
android.R.id.text1, android.R.id.text2
});
setListAdapter(mAdapter);
}
}
——————————————————————————————————————————————转载哦