-------------------------------------------------------------------------------------------------------------------------------------
1-原理,利用ANDROID原生控件ExpandableListview嵌套
2-实现TOP和TWO的适配器
-------------------------------------------------------------TopExpandableListAdapter.java------------------------------------------------------------------------
/**
* 2013-11-22 上午9:39:11 Created By niexiaoqiang
*/
package com.xiaoqiang.test;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.TextView;
import com.xiaoqiang.test.TwoExpandableListAdapter.ItemClickCallback;
/**
* 定义数据结构
*/
public class TopExpandableListAdapter extends BaseExpandableListAdapter {
private Map<String, Map<String, List<ContactBean>>> allContacts;
private Context context;
private AbsListView.LayoutParams textviewLP;
private ItemClickCallback itemClickCallback;
private int contact_row_height;
public TopExpandableListAdapter(int contact_row_height, Context context, Map<String, Map<String, List<ContactBean>>> allContacts, ItemClickCallback itemClickCallback) {
this.context = context;
this.contact_row_height = contact_row_height;
this.itemClickCallback = itemClickCallback;
this.allContacts = allContacts;
textviewLP = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, contact_row_height);
}
@Override
public int getGroupCount() {
// 返回公司的总数
int companyCount = allContacts.size();
return companyCount;
}
@Override
public int getChildrenCount(int groupPosition) {
// 强制返回1,公司下为一个ExpandableListView
return 1;
}
@Override
public Object getGroup(int groupPosition) {
// 返回某个公司的部门的数据
String companyName = (String) allContacts.keySet().toArray()[groupPosition];
Map<String, List<ContactBean>> companyDatas = allContacts.get(companyName);
return companyDatas;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// 返回某个公司的某个部门的数据
@SuppressWarnings("unchecked")
Map<String, List<ContactBean>> companyDatas = (Map<String, List<ContactBean>>) getGroup(childPosition);
String departmentName = (String) companyDatas.keySet().toArray()[childPosition];
// 返回一个返回一个list数据
return companyDatas.get(departmentName);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition * 100;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return groupPosition * 100 + childPosition * 10;
}
@Override
public boolean hasStableIds() {
return false;
}
public ExpandableListView getExpandableListView(Map<String, List<ContactBean>> companyDatas) {
AbsListView.LayoutParams explvLP = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, contact_row_height * companyDatas.size());
final ExpandableListView childExpandableListView = new ExpandableListView(context);
childExpandableListView.setLayoutParams(explvLP);
// 设置绑定
childExpandableListView.setPadding(contact_row_height, 0, 0, 0);
final TwoExpandableListAdapter twoExpandableListAdapter = new TwoExpandableListAdapter(contact_row_height, context, companyDatas, itemClickCallback);
childExpandableListView.setAdapter(twoExpandableListAdapter);
// 收
childExpandableListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
int currentDepartmentcount = twoExpandableListAdapter.getChildrenCount(groupPosition);
AbsListView.LayoutParams lp = (LayoutParams) childExpandableListView.getLayoutParams();
lp.height = lp.height - currentDepartmentcount * contact_row_height;
childExpandableListView.setLayoutParams(lp);
}
});
// 展
childExpandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
int currentDepartmentcount = twoExpandableListAdapter.getChildrenCount(groupPosition);
AbsListView.LayoutParams lp = (LayoutParams) childExpandableListView.getLayoutParams();
lp.height = lp.height + currentDepartmentcount * contact_row_height;
childExpandableListView.setLayoutParams(lp);
}
});
return childExpandableListView;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView textView = new TextView(context);
textView.setLayoutParams(textviewLP);
textView.setPadding(contact_row_height, 0, 0, 0);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setText((String) allContacts.keySet().toArray()[groupPosition]);
return textView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
String companyName = (String) allContacts.keySet().toArray()[groupPosition];
Map<String, List<ContactBean>> companyDatas = allContacts.get(companyName);
ExpandableListView treeView = getExpandableListView(companyDatas);
return treeView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
-------------------------------------------------------------TwoExpandableListAdapter.java------------------------------------------------------------------------
/**
* 2013-11-22 上午11:03:06 Created By niexiaoqiang
*/
package com.xiaoqiang.test;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
/**
* TODO Add Class Description
*/
public class TwoExpandableListAdapter extends BaseExpandableListAdapter {
private Map<String, List<ContactBean>> departmentContacts;
private Context context;
private ItemClickCallback callback;
private int contact_row_height;
public TwoExpandableListAdapter(int contact_row_height, Context context, Map<String, List<ContactBean>> departmentContacts, ItemClickCallback callback) {
this.contact_row_height = contact_row_height;
this.context = context;
this.departmentContacts = departmentContacts;
this.callback = callback;
}
@Override
public int getGroupCount() {
// 得到公司下部门总数
return departmentContacts.size();
}
@Override
public int getChildrenCount(int groupPosition) {
// 得到某部门下,员工总数
return departmentContacts.get(departmentContacts.keySet().toArray()[groupPosition]).size();
}
@Override
public Object getGroup(int groupPosition) {
// 得到某部门下,员工的数据
return departmentContacts.get(departmentContacts.keySet().toArray()[groupPosition]);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return departmentContacts.get(departmentContacts.keySet().toArray()[groupPosition]).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
// 部门*10
return groupPosition * 10;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// 部门*10+员工*1
return groupPosition * 10 + childPosition * 1;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView textView = getTextView(0);
textView.setText((String) departmentContacts.keySet().toArray()[groupPosition]);
return textView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getTextView(contact_row_height);
final ContactBean contactBean = departmentContacts.get(departmentContacts.keySet().toArray()[groupPosition]).get(childPosition);
textView.setText(contactBean.getName());
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (null != callback) {
callback.onClick(contactBean);
}
}
});
return textView;
}
/**
* 得到一个TextView
*
* @param paddingleft
* @return
*/
public TextView getTextView(int paddingleft) {
AbsListView.LayoutParams textviewLP = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, contact_row_height);;
TextView textView = new TextView(context);
textView.setBackgroundResource(R.drawable.contact_selector);
textView.setLayoutParams(textviewLP);
textView.setPadding(contact_row_height, 0, 0, 0);
textView.setGravity(Gravity.CENTER_VERTICAL);
return textView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
public interface ItemClickCallback {
void onClick(ContactBean contactBean);
}
}
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;
import android.widget.Toast;
import com.xiaoqiang.test.TwoExpandableListAdapter.ItemClickCallback;
public class MainActivity extends Activity {
private ExpandableListView contract_company_listcompany;
private int contact_row_height=56;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contract_company_listcompany = (ExpandableListView) this
.findViewById(R.id.contract_company_listcompany);
/******** 测试数据 *************/
Map<String, Map<String, List<ContactBean>>> contacts = new HashMap<String, Map<String, List<ContactBean>>>();
contacts.put("公司1", getCompanydata("公司1"));
contacts.put("公司2", getCompanydata("公司2"));
contacts.put("公司3", getCompanydata("公司3"));
/******** end测试数据 *************/
TopExpandableListAdapter topExpandableListAdapter = new TopExpandableListAdapter(
contact_row_height, this, contacts, itemClickCallback);
contract_company_listcompany.setAdapter(topExpandableListAdapter);
}
/**
* 点击联系人后返回
*/
private ItemClickCallback itemClickCallback = new ItemClickCallback() {
@Override
public void onClick(ContactBean contactBean) {
Toast.makeText(MainActivity.this, contactBean.getName(),
Toast.LENGTH_LONG).show();
}
};
/**
* 测试数据构造器,公司
*
* @return
*/
public Map<String, List<ContactBean>> getCompanydata(String cname) {
Map<String, List<ContactBean>> cdata = new HashMap<String, List<ContactBean>>();
cdata.put(cname + "销售部", getDepartmentdata(cname, "销售部"));
cdata.put(cname + "市场部", getDepartmentdata(cname, "市场部"));
cdata.put(cname + "行政部", getDepartmentdata(cname, "行政部"));
return cdata;
}
/**
* 测试数据返回某个部门下的人员,部门
*
* @param cname
* @param dname
* @return
*/
public List<ContactBean> getDepartmentdata(String cname, String dname) {
List<ContactBean> cdontacts = new ArrayList<ContactBean>();
for (int i = 0; i < 5; i++) {
cdontacts
.add(new ContactBean(cname + "-" + dname + "-" + "小强" + i));
}
return cdontacts;
}
}