Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> andriod 利用ExpandableList做三級樹

andriod 利用ExpandableList做三級樹

編輯:關於Android編程

源碼DEMO 等待審核中.......

\

-------------------------------------------------------------------------------------------------------------------------------------

1-原理,利用ANDROID原生控件ExpandableListview嵌套<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+Mi3Ktc/WVE9Qus1UV0+1xMrKxeTG9zwvcD4KPHA+LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLVRvcEV4cGFuZGFibGVMaXN0QWRhcHRlci5qYXZhLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tPC9wPgo8cD4vKio8YnI+CiAqIDIwMTMtMTEtMjIgyc/O5zk6Mzk6MTEgQ3JlYXRlZCBCeSBuaWV4aWFvcWlhbmc8YnI+CiAqLzxicj4KPGJyPgo8YnI+CnBhY2thZ2UgY29tLnhpYW9xaWFuZy50ZXN0Ozxicj4KPGJyPgo8YnI+CmltcG9ydCBqYXZhLnV0aWwuTGlzdDs8YnI+CmltcG9ydCBqYXZhLnV0aWwuTWFwOzxicj4KPGJyPgo8YnI+CmltcG9ydCBhbmRyb2lkLmNvbnRlbnQuQ29udGV4dDs8YnI+CmltcG9ydCBhbmRyb2lkLnZpZXcuR3Jhdml0eTs8YnI+CmltcG9ydCBhbmRyb2lkLnZpZXcuVmlldzs8YnI+CmltcG9ydCBhbmRyb2lkLnZpZXcuVmlld0dyb3VwOzxicj4KaW1wb3J0IGFuZHJvaWQud2lkZ2V0LkFic0xpc3RWaWV3Ozxicj4KaW1wb3J0IGFuZHJvaWQud2lkZ2V0LkFic0xpc3RWaWV3LkxheW91dFBhcmFtczs8Yn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ID0gKFN0cmluZykgYWxsQ29udGFjdHMua2V5U2V0KCkudG9BcnJheSgpW2dyb3VwUG9zaXRpb25dOzxicj4KTWFwPFN0cmluZywgTGlzdDxDb250YWN0QmVhbj4+IGNvbXBhbnlEYXRhcyA9IGFsbENvbnRhY3RzLmdldChjb21wYW55TmFtZSk7PGJyPgpyZXR1cm4gY29tcGFueURhdGFzOzxicj4KfTxicj4KPGJyPgo8YnI+CkBPdmVycmlkZTxicj4KcHVibGljIE9iamVjdCBnZXRDaGlsZChpbnQgZ3JvdXBQb3NpdGlvbiwgaW50IGNoaWxkUG9zaXRpb24pIHs8YnI+Ci8vILe1u9jEs7j2uavLvrXExLO49rK/w8W1xMr9vt08YnI+CkBTdXBwcmVzc1dhcm5pbmdzKA=="unchecked")
Map> companyDatas = (Map>) 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> 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> 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> departmentContacts;
private Context context;
private ItemClickCallback callback;
private int contact_row_height;


public TwoExpandableListAdapter(int contact_row_height, Context context, Map> 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);
}


}

------------------mainactivity.java----------------------------- package com.xiaoqiang.test;


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>> contacts = new HashMap>>();
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> getCompanydata(String cname) {
Map> cdata = new HashMap>();
cdata.put(cname + "銷售部", getDepartmentdata(cname, "銷售部"));
cdata.put(cname + "市場部", getDepartmentdata(cname, "市場部"));
cdata.put(cname + "行政部", getDepartmentdata(cname, "行政部"));
return cdata;
}


/**
* 測試數據返回某個部門下的人員,部門
*
* @param cname
* @param dname
* @return
*/
public List getDepartmentdata(String cname, String dname) {
List cdontacts = new ArrayList();
for (int i = 0; i < 5; i++) {
cdontacts
.add(new ContactBean(cname + "-" + dname + "-" + "小強" + i));
}
return cdontacts;
}


}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved