如何在base三层expandablelistt中的item中添加按钮

Android UI控件ExpandableListView基本用法详解
作者:生命壹号
字体:[ ] 类型:转载 时间:
这篇文章主要为大家详细介绍了Android UI控件ExpandableListView基本用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
ExpandableListView介绍&
ExpandableListView的引入&
ExpandableListView可以显示一个视图垂直滚动显示两级列表中的条目,这不同于列表视图(ListView)。ExpandableListView允许有两个层次:一级列表中有二级列表。
&比如在手机设置中,对于分类,有很好的效果。手机版QQ也是这样的效果。
使用ExpandableListView的整体思路&
(1)给ExpandableListView设置适配器,那么必须先设置数据源。
&(2)数据源,就是此处的适配器类ExpandableAdapter,此方法继承了BaseExpandableListAdapter,需要重写里面的10个方法。
&数据源中,用到了自定义的View布局,此时根据自己的需求,来设置组和子项的布局样式。
&getChildView()和getGroupView()方法设置自定义布局。&
(3)数据源设置好,直接给ExpandableListView.setAdapter()即可实现此收缩功能。&
ExpandableListView的完整代码实现
&(1)activity_main.xml:在里面放置一个ExpandableListView控件&
&?xml version="1.0" encoding="utf-8"?&
&RelativeLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.smyhvae.expandablelistviewdemo.MainActivity"&
&ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
&/RelativeLayout&
(2)item_group.xml:一级列表的item的布局
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#cccccc"
android:orientation="horizontal"&
android:id="@+id/tv_group"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:gravity="center"
android:text="group text"
android:textColor="#000000"
&/LinearLayout&
(3)item_child.xml:二级列表的item的布局
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"&
&ImageView
android:id="@+id/iv_child"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@mipmap/ic_launcher"/&
android:id="@+id/tv_child"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="item text"
android:textColor="#000000"/&
&/LinearLayout&
(4)MainActivity.java:&
import android.app.A
import android.os.B
import android.util.L
import android.view.V
import android.view.ViewG
import android.widget.BaseExpandableListA
import android.widget.ExpandableListV
import android.widget.ImageV
import android.widget.TextV
public class MainActivity extends Activity {
private ExpandableListView expandableListV
//Model:定义的数据
private String[] groups = {"A", "B", "C"};
//注意,字符数组不要写成{{"A1,A2,A3,A4"}, {"B1,B2,B3,B4,B5"}, {"C1,C2,C3,C4"}}
private String[][] childs = {{"A1", "A2", "A3", "A4"}, {"A1", "A2", "A3", "B4"}, {"A1", "A2", "A3", "C4"}};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
expandableListView.setAdapter(new MyExpandableListView());
//为ExpandableListView自定义适配器
class MyExpandableListView extends BaseExpandableListAdapter {
//返回一级列表的个数
public int getGroupCount() {
return groups.
//返回每个二级列表的个数
public int getChildrenCount(int groupPosition) { //参数groupPosition表示第几个一级列表
Log.d("smyhvae", "--&" + groupPosition);
return childs[groupPosition].
//返回一级列表的单个item(返回的是对象)
public Object getGroup(int groupPosition) {
return groups[groupPosition];
//返回二级列表中的单个item(返回的是对象)
public Object getChild(int groupPosition, int childPosition) {
return childs[groupPosition][childPosition]; //不要误写成groups[groupPosition][childPosition]
public long getGroupId(int groupPosition) {
return groupP
public long getChildId(int groupPosition, int childPosition) {
return childP
//每个item的id是否是固定?一般为true
public boolean hasStableIds() {
//【重要】填充一级列表
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.item_group, null);
TextView tv_group = (TextView) convertView.findViewById(R.id.tv_group);
tv_group.setText(groups[groupPosition]);
return convertV
//【重要】填充二级列表
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.item_child, null);
ImageView iv_child = (ImageView) convertView.findViewById(R.id.iv_child);
TextView tv_child = (TextView) convertView.findViewById(R.id.tv_child);
//iv_child.setImageResource(resId);
tv_child.setText(childs[groupPosition][childPosition]);
return convertV
//二级列表中的item是否能够被选中?可以改为true
public boolean isChildSelectable(int groupPosition, int childPosition) {
注:请自行完成ConvertView和ViewHolder的优化。&
工程文件:(Android Studio 2.1)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具如何在QListWidgetItem中添加按钮?
[问题点数:90分,结帖人coderlaw]
如何在QListWidgetItem中添加按钮?
[问题点数:90分,结帖人coderlaw]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
2012年10月 移动平台大版内专家分月排行榜第三2012年2月 移动平台大版内专家分月排行榜第三
2012年10月 移动平台大版内专家分月排行榜第三2012年2月 移动平台大版内专家分月排行榜第三
2012年10月 移动平台大版内专家分月排行榜第三2012年2月 移动平台大版内专家分月排行榜第三
2012年9月 移动平台大版内专家分月排行榜第二
2012年11月 移动平台大版内专家分月排行榜第三
2012年10月 移动平台大版内专家分月排行榜第三2012年2月 移动平台大版内专家分月排行榜第三
本帖子已过去太久远了,不再提供回复功能。4063人阅读
Android(2)
android中ExpandableListView增加自定义组件,点击ExpandableListView自动滚动到顶部
MainActivity.java
package cn.android.
import java.util.ArrayL
import java.util.HashM
import java.util.L
import org.custom.widget.R;
import android.app.A
import android.os.B
import android.view.V
import android.widget.B
import android.widget.ExpandableListV
import android.widget.ExpandableListView.OnChildClickL
import android.widget.ExpandableListView.OnGroupExpandL
import android.widget.T
@SuppressWarnings({&unused&, &rawtypes&})
public class MainActivity extends Activity {
ExpandableListView expandableL
public List&HashMap&String, Object&&
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setCategoris();
adapter = new TreeViewAdapter(this);
expandableList = (ExpandableListView) findViewById(R.id.category_items);
expandableList.setGroupIndicator(null);
displayCategories();
public void displayCategories() {
adapter.RemoveAll();
adapter.notifyDataSetChanged();
List&TreeViewAdapter.TreeNode& treeNode = adapter.GetTreeNode();
for (int i = 0; i & categories.size(); i++) {
TreeViewAdapter.TreeNode node = new TreeViewAdapter.TreeNode();
node.parent = categories.get(i);
List child = ((ArrayList) categories.get(i).get(&children&));
for (int ii = 0; ii & child.size(); ii++) {
node.childs.add(child.get(ii));
treeNode.add(node);
adapter.UpdateTreeNode(treeNode);
expandableList.setAdapter(adapter);
expandableList.setOnGroupExpandListener(new OnGroupExpandListener() {
public void onGroupExpand(int groupPosition) {
if(groupPosition != 0){
expandableList.setSelectedGroup(groupPosition);
expandableList.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView arg0, View arg1,
int parent, int children, long arg4) {
String str = &parent id:& + String.valueOf(parent)
+ &,children id:& + String.valueOf(children);
HashMap map = (HashMap) ((ArrayList) categories.get(parent).get(&children&)).get(children);
int catId = (Integer) map.get(&cat_id&);
String categoryName = (String) map.get(&category_name&);
Toast.makeText(MainActivity.this, &cat_id:& + catId + &, category_name:& + categoryName, Toast.LENGTH_SHORT).show();
public void setCategoris() {
categories = new ArrayList&HashMap&String,Object&&();
HashMap&String, Object& mapFather1 = new HashMap&String, Object&();
mapFather1.put(&cat_id&, 1);
mapFather1.put(&drawable&, R.drawable.category_new_in_icon);
mapFather1.put(&category_name&, &New In&);
mapFather1.put(&description&, &Dress/Coats/Lace&);
ArrayList&HashMap&String, Object&& son1 = new ArrayList&HashMap&String,Object&&();
HashMap&String, Object& mapSon11 = new HashMap&String, Object&();
mapSon11.put(&cat_id&, 6);
mapSon11.put(&drawable&, R.drawable.category_dresses1_icon);
mapSon11.put(&category_name&, &Dress&);
HashMap&String, Object& mapSon12 = new HashMap&String, Object&();
mapSon12.put(&cat_id&, 8);
mapSon12.put(&drawable&, R.drawable.category_women_coats_icon);
mapSon12.put(&category_name&, &Coats&);
HashMap&String, Object& mapSon13 = new HashMap&String, Object&();
mapSon13.put(&cat_id&, 12);
mapSon13.put(&drawable&, R.drawable.category_lace1_icon);
mapSon13.put(&category_name&, &Lace&);
HashMap&String, Object& mapSon14 = new HashMap&String, Object&();
mapSon14.put(&cat_id&, 12);
mapSon14.put(&drawable&, R.drawable.category_chiffon1_icon);
mapSon14.put(&category_name&, &Chiffon&);
son1.add(mapSon11);
son1.add(mapSon12);
son1.add(mapSon13);
son1.add(mapSon14);
mapFather1.put(&children&, son1);
HashMap&String, Object& mapFather2 = new HashMap&String, Object&();
mapFather2.put(&cat_id&, 2);
mapFather2.put(&drawable&, R.drawable.category_collection_icon);
mapFather2.put(&category_name&, &Shop Collection&);
mapFather2.put(&description&, &Wintage/Going out/Workwear&);
ArrayList&HashMap&String, Object&& son2 = new ArrayList&HashMap&String,Object&&();
HashMap&String, Object& mapSon21 = new HashMap&String, Object&();
mapSon21.put(&cat_id&, 21);
mapSon21.put(&drawable&, R.drawable.category_vintage1_icon);
mapSon21.put(&category_name&, &Vintage&);
HashMap&String, Object& mapSon22 = new HashMap&String, Object&();
mapSon22.put(&cat_id&, 22);
mapSon22.put(&drawable&, R.drawable.category_going_out1_icon);
mapSon22.put(&category_name&, &Going out&);
HashMap&String, Object& mapSon23 = new HashMap&String, Object&();
mapSon23.put(&cat_id&, 23);
mapSon23.put(&drawable&, R.drawable.category_workwear1_icon);
mapSon23.put(&category_name&, &Workwear&);
HashMap&String, Object& mapSon24 = new HashMap&String, Object&();
mapSon24.put(&cat_id&, 24);
mapSon24.put(&drawable&, R.drawable.category_designer1_icon);
mapSon24.put(&category_name&, &Designer&);
HashMap&String, Object& mapSon25 = new HashMap&String, Object&();
mapSon25.put(&cat_id&, 24);
mapSon25.put(&drawable&, R.drawable.category_sweety1_icon);
mapSon25.put(&category_name&, &Sweety&);
HashMap&String, Object& mapSon26 = new HashMap&String, Object&();
mapSon26.put(&cat_id&, 24);
mapSon26.put(&drawable&, R.drawable.category_maxi_dress1_icon);
mapSon26.put(&category_name&, &Maxi Dress&);
HashMap&String, Object& mapSon27 = new HashMap&String, Object&();
mapSon27.put(&cat_id&, 24);
mapSon27.put(&drawable&, R.drawable.category_night_club1_icon);
mapSon27.put(&category_name&, &Night Club&);
HashMap&String, Object& mapSon28 = new HashMap&String, Object&();
mapSon28.put(&cat_id&, 24);
mapSon28.put(&drawable&, R.drawable.category_print_floral1_icon);
mapSon28.put(&category_name&, &Print/Floral&);
HashMap&String, Object& mapSon29 = new HashMap&String, Object&();
mapSon29.put(&cat_id&, 24);
mapSon29.put(&drawable&, R.drawable.category_party1_icon);
mapSon29.put(&category_name&, &Party&);
son2.add(mapSon21);
son2.add(mapSon22);
son2.add(mapSon23);
son2.add(mapSon24);
son2.add(mapSon25);
son2.add(mapSon26);
son2.add(mapSon27);
son2.add(mapSon28);
son2.add(mapSon29);
mapFather2.put(&children&, son2);
HashMap&String, Object& mapFather3 = new HashMap&String, Object&();
mapFather3.put(&cat_id&, 3);
mapFather3.put(&drawable&, R.drawable.category_women_icon);
mapFather3.put(&category_name&, &Clothing&);
mapFather3.put(&description&, &Dress/Coats/Lace&);
ArrayList&HashMap&String, Object&& son3 = new ArrayList&HashMap&String,Object&&();
HashMap&String, Object& mapson31 = new HashMap&String, Object&();
mapson31.put(&cat_id&, 6);
mapson31.put(&drawable&, R.drawable.category_dresses1_icon);
mapson31.put(&category_name&, &Dress&);
HashMap&String, Object& mapson32 = new HashMap&String, Object&();
mapson32.put(&cat_id&, 8);
mapson32.put(&drawable&, R.drawable.category_women_coats_icon);
mapson32.put(&category_name&, &Coats&);
HashMap&String, Object& mapson33 = new HashMap&String, Object&();
mapson33.put(&cat_id&, 33);
mapson33.put(&drawable&, R.drawable.category_blouses_icon);
mapson33.put(&category_name&, &Blouses&);
HashMap&String, Object& mapson34 = new HashMap&String, Object&();
mapson34.put(&cat_id&, 34);
mapson34.put(&drawable&, R.drawable.category_women_tees_icon);
mapson34.put(&category_name&, &Tees/Vests&);
HashMap&String, Object& mapson35 = new HashMap&String, Object&();
mapson35.put(&cat_id&, 35);
mapson35.put(&drawable&, R.drawable.category_women_sweaters_icon);
mapson35.put(&category_name&, &Sweaters/Cardigans&);
HashMap&String, Object& mapson36 = new HashMap&String, Object&();
mapson36.put(&cat_id&, 36);
mapson36.put(&drawable&, R.drawable.category_women_pants_icon);
mapson36.put(&category_name&, &Pants/Shorts&);
HashMap&String, Object& mapson37 = new HashMap&String, Object&();
mapson37.put(&cat_id&, 37);
mapson37.put(&drawable&, R.drawable.category_women_skirt_icon);
mapson37.put(&category_name&, &Skirts&);
HashMap&String, Object& mapson38 = new HashMap&String, Object&();
mapson38.put(&cat_id&, 38);
mapson38.put(&drawable&, R.drawable.category_women_leggings_icon);
mapson38.put(&category_name&, &Leggings&);
HashMap&String, Object& mapson39 = new HashMap&String, Object&();
mapson39.put(&cat_id&, 39);
mapson39.put(&drawable&, R.drawable.category_women_jumpsuits_icon);
mapson39.put(&category_name&, &Jumpsuits&);
HashMap&String, Object& mapson40 = new HashMap&String, Object&();
mapson40.put(&cat_id&, 40);
mapson40.put(&drawable&, R.drawable.category_women_formal_dresses_icon);
mapson40.put(&category_name&, &Formal Dress&);
HashMap&String, Object& mapson72 = new HashMap&String, Object&();
mapson72.put(&cat_id&, 72);
mapson72.put(&drawable&, R.drawable.category_women_shapewear);
mapson72.put(&category_name&, &Shapewear/Corset&);
HashMap&String, Object& mapson73 = new HashMap&String, Object&();
mapson73.put(&cat_id&, 73);
mapson73.put(&drawable&, R.drawable.category_women_swimwear);
mapson73.put(&category_name&, &Swimwear&);
son3.add(mapson31);
son3.add(mapson32);
son3.add(mapson33);
son3.add(mapson34);
son3.add(mapson35);
son3.add(mapson36);
son3.add(mapson37);
son3.add(mapson38);
son3.add(mapson39);
son3.add(mapson40);
son3.add(mapson72);
son3.add(mapson73);
mapFather3.put(&children&, son3);
HashMap&String, Object& mapFather4 = new HashMap&String, Object&();
mapFather4.put(&cat_id&, 4);
mapFather4.put(&drawable&, R.drawable.category_shoes_icon);
mapFather4.put(&category_name&, &Shoes&);
mapFather4.put(&description&, &Sandals/Heels/Flats&);
ArrayList&HashMap&String, Object&& son4 = new ArrayList&HashMap&String,Object&&();
HashMap&String, Object& mapson41 = new HashMap&String, Object&();
mapson41.put(&cat_id&, 60);
mapson41.put(&drawable&, R.drawable.category_shoes_sandals_icon);
mapson41.put(&category_name&, &Sandals&);
HashMap&String, Object& mapson42 = new HashMap&String, Object&();
mapson42.put(&cat_id&, 42);
mapson42.put(&drawable&, R.drawable.category_shoes_heels_icon);
mapson42.put(&category_name&, &Heels&);
HashMap&String, Object& mapson43 = new HashMap&String, Object&();
mapson43.put(&cat_id&, 43);
mapson43.put(&drawable&, R.drawable.category_shoes_flats_icon);
mapson43.put(&category_name&, &Flats&);
HashMap&String, Object& mapson44 = new HashMap&String, Object&();
mapson44.put(&cat_id&, 44);
mapson44.put(&drawable&, R.drawable.category_shoes_boots_icon);
mapson44.put(&category_name&, &Boots&);
son4.add(mapson41);
son4.add(mapson42);
son4.add(mapson43);
son4.add(mapson44);
mapFather4.put(&children&, son4);
HashMap&String, Object& mapFather5 = new HashMap&String, Object&();
mapFather5.put(&cat_id&, 5);
mapFather5.put(&drawable&, R.drawable.category_bags_icon);
mapFather5.put(&category_name&, &Bags&);
mapFather5.put(&description&, &Dress/Coats/Lace&);
ArrayList&HashMap&String, Object&& son5 = new ArrayList&HashMap&String,Object&&();
HashMap&String, Object& mapson51 = new HashMap&String, Object&();
mapson51.put(&cat_id&, 51);
mapson51.put(&drawable&, R.drawable.category_handbags_icon);
mapson51.put(&category_name&, &Handbags&);
HashMap&String, Object& mapson52 = new HashMap&String, Object&();
mapson52.put(&cat_id&, 52);
mapson52.put(&drawable&, R.drawable.category_shoulder_bags_icon);
mapson52.put(&category_name&, &Shoulder Bags&);
HashMap&String, Object& mapson53 = new HashMap&String, Object&();
mapson53.put(&cat_id&, 53);
mapson53.put(&drawable&, R.drawable.category_backpacks_icon);
mapson53.put(&category_name&, &Backpacks&);
HashMap&String, Object& mapson54 = new HashMap&String, Object&();
mapson54.put(&cat_id&, 54);
mapson54.put(&drawable&, R.drawable.category_clutches_icon);
mapson54.put(&category_name&, &Clutches/Wallets&);
son5.add(mapson51);
son5.add(mapson52);
son5.add(mapson53);
son5.add(mapson54);
mapFather5.put(&children&, son5);
HashMap&String, Object& mapFather6 = new HashMap&String, Object&();
mapFather6.put(&cat_id&, 6);
mapFather6.put(&drawable&, R.drawable.category_accessorie_icon);
mapFather6.put(&category_name&, &Accessories&);
mapFather6.put(&description&, &Holidays/Necklace/Earrings&);
ArrayList&HashMap&String, Object&& son6 = new ArrayList&HashMap&String,Object&&();
HashMap&String, Object& mapson61 = new HashMap&String, Object&();
mapson61.put(&cat_id&, 61);
mapson61.put(&drawable&, R.drawable.category_holidays_icon);
mapson61.put(&category_name&, &Holidays&);
HashMap&String, Object& mapson62 = new HashMap&String, Object&();
mapson62.put(&cat_id&, 62);
mapson62.put(&drawable&, R.drawable.category_accesories_necklace_icon);
mapson62.put(&category_name&, &Necklace&);
HashMap&String, Object& mapson63 = new HashMap&String, Object&();
mapson63.put(&cat_id&, 63);
mapson63.put(&drawable&, R.drawable.category_accesories_earrings_icon);
mapson63.put(&category_name&, &Earrings&);
HashMap&String, Object& mapson64 = new HashMap&String, Object&();
mapson64.put(&cat_id&, 64);
mapson64.put(&drawable&, R.drawable.category_accesories_brooch_icon);
mapson64.put(&category_name&, &Brooch&);
HashMap&String, Object& mapson65 = new HashMap&String, Object&();
mapson65.put(&cat_id&, 65);
mapson65.put(&drawable&, R.drawable.category_accesories_bracelets_icon);
mapson65.put(&category_name&, &Bracelet&);
HashMap&String, Object& mapson66 = new HashMap&String, Object&();
mapson66.put(&cat_id&, 66);
mapson66.put(&drawable&, R.drawable.category_hair_icon);
mapson66.put(&category_name&, &Hair Accessories&);
HashMap&String, Object& mapson67 = new HashMap&String, Object&();
mapson67.put(&cat_id&, 67);
mapson67.put(&drawable&, R.drawable.category_silk_scarves_icon);
mapson67.put(&category_name&, &Scarves&);
son6.add(mapson61);
son6.add(mapson62);
son6.add(mapson63);
son6.add(mapson64);
son6.add(mapson65);
son6.add(mapson66);
son6.add(mapson67);
mapFather6.put(&children&, son6);
HashMap&String, Object& mapFather7 = new HashMap&String, Object&();
mapFather7.put(&cat_id&, 7);
mapFather7.put(&drawable&, R.drawable.category_recommendation_icon);
mapFather7.put(&category_name&, &Recommendation&);
mapFather7.put(&description&, &Hot/New&);
ArrayList&HashMap&String, Object&& son7 = new ArrayList&HashMap&String,Object&&();
HashMap&String, Object& mapson71 = new HashMap&String, Object&();
mapson71.put(&cat_id&, 71);
mapson71.put(&drawable&, R.drawable.category_hot_icon);
mapson71.put(&category_name&, &Hot&);
HashMap&String, Object& mapson720 = new HashMap&String, Object&();
mapson720.put(&cat_id&, 72);
mapson720.put(&drawable&, R.drawable.category_new_icon);
mapson720.put(&category_name&, &New&);
son7.add(mapson71);
son7.add(mapson720);
mapFather7.put(&children&, son7);
categories.add(mapFather1);
categories.add(mapFather2);
categories.add(mapFather3);
categories.add(mapFather4);
categories.add(mapFather5);
categories.add(mapFather6);
categories.add(mapFather7);
TreeViewAdapter.java
package cn.android.
import java.util.ArrayL
import java.util.HashM
import java.util.L
import org.custom.widget.R;
import android.content.C
import android.util.AttributeS
import android.view.G
import android.view.V
import android.view.ViewG
import android.widget.AbsListV
import android.widget.BaseExpandableListA
@SuppressWarnings({&rawtypes&})
public class TreeViewAdapter extends BaseExpandableListAdapter {
public static final int itemHeight = 140;// 每项的高度
static public class TreeNode {
List&Object& childs = new ArrayList&Object&();
List&TreeNode& treeNodes = new ArrayList&TreeNode&();
Context parentC
public TreeViewAdapter(Context view) {
parentContext =
public List&TreeNode& GetTreeNode() {
return treeN
public void UpdateTreeNode(List&TreeNode& nodes) {
treeNodes =
public void RemoveAll() {
treeNodes.clear();
public HashMap getChild(int groupPosition, int childPosition) {
return (HashMap) treeNodes.get(groupPosition).childs.get(childPosition);
public int getChildrenCount(int groupPosition) {
return treeNodes.get(groupPosition).childs.size();
static public CustomImageButton getTextView(Context context, int itemHeight) {
AttributeSet attrs =
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, itemHeight);
CustomImageButton imageButton = new CustomImageButton(context, attrs);
imageButton.setLayoutParams(lp);
imageButton.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
return imageB
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
CustomImageButton imageButton = getTextView(this.parentContext, itemHeight - 30);
imageButton.setImageResource((Integer) getChild(groupPosition, childPosition).get(&drawable&));
imageButton.setTextViewText((String) getChild(groupPosition, childPosition).get(&category_name&));
imageButton.setTextViewDescriptionVisibility(true);
imageButton.setImageViewArrowVisibility(true);
imageButton.setTextSize(12.0f);
imageButton.setBackgroundColor(android.graphics.Color.WHITE);
return imageB
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
CustomImageButton imageButton = getTextView(this.parentContext, itemHeight);
imageButton.setImageResource((Integer) getGroup(groupPosition).get(&drawable&));
imageButton.setTextViewText((String) getGroup(groupPosition).get(&category_name&));
imageButton.setTextViewDescription((String) getGroup(groupPosition).get(&description&));
imageButton.setDescriptionColor(0xFF5D5D5D);
imageButton.setDescriptionSize(10.0f);
imageButton.setBackgroundColor(parentContext.getResources().getColor(R.color.gray));
return imageB
public long getChildId(int groupPosition, int childPosition) {
return childP
public HashMap getGroup(int groupPosition) {
return (HashMap) treeNodes.get(groupPosition).
public int getGroupCount() {
return treeNodes.size();
public long getGroupId(int groupPosition) {
return groupP
public boolean isChildSelectable(int groupPosition, int childPosition) {
public boolean hasStableIds() {
CustomImageButton.java
package cn.android.
import org.custom.widget.R;
import android.content.C
import android.util.AttributeS
import android.view.LayoutI
import android.widget.ImageV
import android.widget.RelativeL
import android.widget.TextV
public class CustomImageButton extends RelativeLayout {
private ImageView imageV
private TextView textV
private TextView textD
private ImageView imageViewA
public CustomImageButton(Context context) {
super(context);
public CustomImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_image_button, this);
imageView = (ImageView) findViewById(R.id.custom_image_button_thumb);
textView = (TextView) findViewById(R.id.custom_image_button_text);
textDescription = (TextView) findViewById(R.id.custom_image_button_description);
imageViewArrow = (ImageView) findViewById(R.id.custom_image_view_arrow);
* 设置图片资源
public void setImageResource(int resId) {
imageView.setImageResource(resId);
* 设置显示的文字
public void setTextViewText(String text) {
textView.setText(text);
* 设置显示的文字的说明
public void setTextViewDescription(String text) {
textDescription.setText(text);
* 设置显示的文字的说明是否隐藏
public void setTextViewDescriptionVisibility(boolean bool) {
if (bool) {
textDescription.setVisibility(GONE);
* 设置显示的右侧箭头是否显示
public void setImageViewArrowVisibility(boolean bool) {
if (bool) {
imageViewArrow.setVisibility(VISIBLE);
* 设置显示的文字字体大小
public void setTextSize(float size) {
textView.setTextSize(size);
* 设置显示简介的文字字体大小
public void setDescriptionSize(float size) {
textDescription.setTextSize(size);
* 设置显示的文字字体颜色
public void setTextColor(int color) {
textView.setTextColor(color);
* 设置显示的简介文字字体颜色
public void setDescriptionColor(int color) {
textDescription.setTextColor(color);
custom_image_button.xml
&?xml version=&1.0& encoding=&utf-8&?&
&RelativeLayout xmlns:android=&/apk/res/android&
android:id=&@id/custom_image_button_layout&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:gravity=&center_vertical&&
&ImageView
android:id=&@id/custom_image_button_thumb&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_gravity=&center_vertical&
android:paddingBottom=&0.0dip&
android:paddingLeft=&0.0dip&
android:paddingTop=&0.0dip&
android:src=&@drawable/category_new_in_icon& /&
android:id=&@id/custom_image_button_text&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_gravity=&center_vertical&
android:layout_marginLeft=&15dip&
android:text=&button&
android:textColor=&@color/black&
android:paddingTop=&10dip&
android:layout_toRightOf=&@id/custom_image_button_thumb& /&
android:id=&@id/custom_image_button_description&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:layout_gravity=&center_vertical&
android:layout_marginLeft=&15dip&
android:text=&button&
android:textColor=&@color/black&
android:paddingTop=&3dip&
android:layout_toRightOf=&@id/custom_image_button_thumb&
android:layout_below=&@id/custom_image_button_text& /&
&ImageView
android:id=&@id/custom_image_view_arrow&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:gravity=&center_vertical&
android:paddingTop=&13.0dip&
android:paddingRight=&5.0dip&
android:layout_alignParentRight=&true&
android:src=&@drawable/categories_right_arrow&
android:visibility=&gone& /&
&/RelativeLayout&
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:orientation=&vertical& &
&ExpandableListView
android:id=&@id/category_items&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:cacheColorHint=&#&&
&/ExpandableListView&
&/LinearLayout&
colors.xml
&?xml version=&1.0& encoding=&UTF-8&?&
&resources&
&item type=&color& name=&black&&#FF000000&/item&
&color name=&gray&&#FFE7E7E7&/color&
&/resources&
&?xml version=&1.0& encoding=&utf-8&?&
&resources&
&item type=&id& name=&category_items&/&
&item type=&id& name=&custom_image_button_layout&/&
&item type=&id& name=&custom_image_button_thumb&/&
&item type=&id& name=&custom_image_button_text&/&
&item type=&id& name=&custom_image_button_description&/&
&item type=&id& name=&custom_image_view_arrow&/&
&/resources&
下载地址:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:76662次
排名:千里之外
原创:21篇
转载:12篇
评论:30条
(3)(2)(1)(1)(1)(1)(1)(1)(2)(4)(18)
() () () ()}

我要回帖

更多关于 xexpandablelistview 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信