本文共 11626 字,大约阅读时间需要 38 分钟。
在项目开发中,也许我们遇到过ListView中嵌套ListView,但谷歌建议我们最好别这样做,因此他们写好了一个ExpandableListView类,他继承ListView,可以实现ListView中嵌套ListView的效果,好了,废话不多说,先上效果图:
点击下载源码:
主代码:
- public class ExListView extends Activity {
- private static final String GROUP_TEXT = "group_text";
- private static final String CHILD_TEXT1 = "child_text1";
- private static final String CHILD_TEXT2 = "child_text2";
-
- List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
- List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
-
- ExAdapter adapter;
- ExpandableListView exList;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- for (int i = 1; i < 6; i++) {
- Map<String, String> curGroupMap = new HashMap<String, String>();
- groupData.add(curGroupMap);
- curGroupMap.put(GROUP_TEXT, "第" + i + "大组");
-
- List<Map<String, String>> children = new ArrayList<Map<String, String>>();
- for (int j = 1; j < 6; j++) {
- Map<String, String> curChildMap = new HashMap<String, String>();
- children.add(curChildMap);
- curChildMap.put(CHILD_TEXT1, "第" + j + "小组");
- curChildMap.put(CHILD_TEXT2, "第" + j + "小组签名");
- }
- childData.add(children);
- }
-
- adapter = new ExAdapter(ExListView.this);
- exList = (ExpandableListView) findViewById(R.id.list);
- exList.setAdapter(adapter);
- exList.setGroupIndicator(null);
- exList.setDivider(null);
- }
-
- class ExAdapter extends BaseExpandableListAdapter {
- Context context;
-
- public ExAdapter(Context context) {
- super();
- this.context = context;
- }
-
- public View getGroupView(int groupPosition, boolean isExpanded,
- View convertView, ViewGroup parent) {
- View view = convertView;
- if (view == null) {
- LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- view = inflater.inflate(R.layout.member_listview, null);
- }
-
- TextView title = (TextView) view.findViewById(R.id.content_001);
- title.setText(getGroup(groupPosition).toString());
-
- ImageView image = (ImageView) view.findViewById(R.id.tubiao);
- if (isExpanded)
- image.setBackgroundResource(R.drawable.btn_browser2);
- else
- image.setBackgroundResource(R.drawable.btn_browser);
-
- return view;
- }
-
- public long getGroupId(int groupPosition) {
- return groupPosition;
- }
-
- public Object getGroup(int groupPosition) {
- return groupData.get(groupPosition).get(GROUP_TEXT).toString();
- }
-
- public int getGroupCount() {
- return groupData.size();
-
- }
-
-
- public View getChildView(int groupPosition, int childPosition,
- boolean isLastChild, View convertView, ViewGroup parent) {
- View view = convertView;
- if (view == null) {
- LayoutInflater inflater = LayoutInflater.from(context);
- view = inflater.inflate(R.layout.member_childitem, null);
- }
- final TextView title = (TextView) view
- .findViewById(R.id.child_text);
- title.setText(childData.get(groupPosition).get(childPosition)
- .get(CHILD_TEXT1).toString());
- final TextView title2 = (TextView) view
- .findViewById(R.id.child_text2);
- title2.setText(childData.get(groupPosition).get(childPosition)
- .get(CHILD_TEXT2).toString());
-
- return view;
- }
-
- public long getChildId(int groupPosition, int childPosition) {
- return childPosition;
- }
-
- public Object getChild(int groupPosition, int childPosition) {
- return childData.get(groupPosition).get(childPosition).get(CHILD_TEXT1)
- .toString();
- }
-
- public int getChildrenCount(int groupPosition) {
- return childData.get(groupPosition).size();
- }
-
-
-
-
-
-
-
- public boolean hasStableIds() {
- return true;
- }
-
- public boolean isChildSelectable(int groupPosition, int childPosition) {
- return true;
- }
-
- }
- }
public class ExListView extends Activity { private static final String GROUP_TEXT = "group_text";//大组成员Map的key private static final String CHILD_TEXT1 = "child_text1";//小组成员Map的第一个key private static final String CHILD_TEXT2 = "child_text2";//小组成员Map的第二个key List
主界面配置文件main.xml:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/default_bg" >
-
- <ExpandableListView
- android:id="@+id/list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_alignParentLeft="true" android:cacheColorHint="#00000000" />
-
- </RelativeLayout>
大组成员配置文件member_listview.xml:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal" >
-
- <LinearLayout
- android:id="@+id/layout_013"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
-
- <ImageView
- android:id="@+id/ImageView01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:gravity="center_vertical"
- android:paddingTop="10dp"
- android:src="@drawable/user_group" >
- </ImageView>
-
- <RelativeLayout
- android:id="@+id/layout_013"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
-
- <TextView
- android:id="@+id/content_001"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_gravity="center_vertical"
- android:gravity="center_vertical"
- android:paddingLeft="10dp"
- android:textColor="#FFFFFF"
- android:textSize="30sp" >
- </TextView>
-
- <ImageView
- android:id="@+id/tubiao"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true" >
- </ImageView>
- </RelativeLayout>
- </LinearLayout>
-
- <!--
- <RelativeLayout android:id="@+id/layout_013"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ImageView android:id="@+id/ImageView01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/icon"></ImageView>
- <TextView android:id="@+id/content_001"
- android:text="@+id/TextView01"
- android:layout_width="wrap_content"
- android:layout_toRightOf="@+id/ImageView01"
- android:layout_height="wrap_content"></TextView>
- <ImageView android:layout_width="wrap_content"
- android:layout_toRightOf="@+id/content_001"
- android:layout_height="wrap_content"
- android:id="@+id/tubiao"></ImageView>
- </RelativeLayout>
-
-
- -->
-
- </LinearLayout>
小组成员配置文件member_childitem.xml:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/childlayout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal" >
-
- <ImageView
- android:id="@+id/child_image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="40dp"
- android:background="@drawable/child_image"
- android:paddingTop="10dp" >
- </ImageView>
-
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
-
- <TextView
- android:id="@+id/child_text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:gravity="center_vertical"
- android:text=""
- android:textColor="#FFFFFF"
- android:textSize="25sp" >
- </TextView>
-
- <TextView
- android:id="@+id/child_text2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:gravity="center_vertical"
- android:text=""
- android:textColor="#FFFFFF"
- android:textSize="20sp" >
- </TextView>
- </LinearLayout>
-
- </LinearLayout>
好了,今天就到这里,中午休息一会。。。
转载地址:http://ywbsi.baihongyu.com/