Android学习之ExpandableListView

时间:2021-12-14 00:37:24


ExpListActivity.java:

 

package org.lee.android;

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.widget.ExpandableListView;

public class ExpListActivity extends Activity {

private ExpandableListView elistview;
private List<Map<String, Object>> mData;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.exp_listview);
mData = getData();
elistview = (ExpandableListView) findViewById(R.id.elist);
//这里要把系统自带的图标去掉
elistview.setGroupIndicator(null);
elistview.setAdapter(new MyElistAdapter(this,mData));
// elistview.setChildDivider(null);
// elistview.setDivider(null);
}

private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
List<Map<String, Object>> clist = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> cmap = new HashMap<String, Object>();
cmap.put("cname", "安庆市");
clist.add(cmap);
cmap = new HashMap<String, Object>();
cmap.put("cname", "合肥市");
clist.add(cmap);
cmap = new HashMap<String, Object>();
cmap.put("cname", "池州市");
clist.add(cmap);
cmap = new HashMap<String, Object>();
cmap.put("cname", "宿州市");
clist.add(cmap);
map.put("gname", "安徽省");
map.put("clist", clist);
list.add(map);

clist = new ArrayList<Map<String, Object>>();
map = new HashMap<String, Object>();
cmap = new HashMap<String, Object>();
cmap.put("cname", "南京市");
clist.add(cmap);
cmap = new HashMap<String, Object>();
cmap.put("cname", "镇江市");
clist.add(cmap);
cmap = new HashMap<String, Object>();
cmap.put("cname", "徐州市");
clist.add(cmap);
map.put("gname", "江苏省");
map.put("clist", clist);
list.add(map);

clist = new ArrayList<Map<String, Object>>();
map = new HashMap<String, Object>();
cmap = new HashMap<String, Object>();
cmap.put("cname", "武汉市");
clist.add(cmap);
cmap = new HashMap<String, Object>();
cmap.put("cname", "黄冈市");
clist.add(cmap);
cmap = new HashMap<String, Object>();
cmap.put("cname", "宜昌市");
clist.add(cmap);
map.put("gname", "湖北省");
map.put("clist", clist);
list.add(map);


return list;
}
}

 

 

 

 

 

MyElistAdapter.java:

 

package org.lee.android;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MyElistAdapter extends BaseExpandableListAdapter {
// 分组数据
// private String[] group = { "A组", "B组", "C组", "D组" };
// private String[][] child = { { "A01", "A02", "A03" },
// { "B01", "B02", "B03" }, { "C01", "C02", "C03" },
// { "D04", "D05", "D06" } };
private Context mContext;
private List<Map<String, Object>> glist;

public MyElistAdapter(Context mContext,List<Map<String, Object>> glist) {
super();
this.mContext = mContext;
this.glist=glist;
}

@Override
public int getGroupCount() {
return glist.size();
}

@SuppressWarnings("unchecked")
@Override
public int getChildrenCount(int groupPosition) {
List<Map<String, Object>> clist=(List<Map<String, Object>>) glist.get(groupPosition).get("clist");
return clist.size();
}

@Override
public Object getGroup(int groupPosition) {
return glist.get(groupPosition).get("gname");
}

@SuppressWarnings("unchecked")
@Override
public Object getChild(int groupPosition, int childPosition) {
List<Map<String, Object>> clist=(List<Map<String, Object>>) glist.get(groupPosition).get("clist");
return clist.get(childPosition).get("cname");
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public boolean hasStableIds() {
return true;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// 实例化布局文件
RelativeLayout glayout = (RelativeLayout) LayoutInflater.from(mContext)
.inflate(R.layout.group_layout, null);
ImageView iv = (ImageView) glayout.findViewById(R.id.giv);
// 判断分组是否展开,分别传入不同的图片资源
if (isExpanded) {
iv.setImageResource(R.drawable.shop_cart_cut);
} else {
iv.setImageResource(R.drawable.shop_cart_plus);
}
TextView tv = (TextView) glayout.findViewById(R.id.gtv);
tv.setText(this.getGroup(groupPosition).toString());
return glayout;
}

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// 实例化布局文件
LinearLayout clayout = (LinearLayout) LayoutInflater.from(mContext)
.inflate(R.layout.child_layout, null);
TextView tv = (TextView) clayout.findViewById(R.id.ctv);
tv.setText(getChild(groupPosition, childPosition).toString());
return clayout;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}

 

 

 

 

 

exp_listview.xml:

 


<?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="fill_parent"
android:orientation="vertical"
android:background="#ffffff">

<ExpandableListView
android:id="@+id/elist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
/>

</LinearLayout>



 

 

 

 

 

group_layout.xml:

 


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grlayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#ffffff"
android:orientation="horizontal">

<TextView
android:id="@+id/gtv"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:paddingLeft="20dp"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:textColor="#000000"
/>

<ImageView
android:id="@+id/giv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingRight="10dp"
android:src="@drawable/shop_cart_plus"
/>

</RelativeLayout>



 

 

 

 

 

child_layout.xml:

 



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#ffffff"
android:paddingLeft="30dp"
>

<ImageView
android:id="@+id/civ"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/icon_gcoding"
android:padding="5dp"
/>

<TextView
android:id="@+id/ctv"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="5dp"
android:textColor="#000000"
/>

</LinearLayout>



 

 

Android学习之ExpandableListView