android中ExpandableListView增加自定义组件,点击ExpandableListView自动滚动到顶部
MainActivity.java
package cn.android.widget; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.custom.widget.R; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ExpandableListView.OnGroupExpandListener; import android.widget.Toast; @SuppressWarnings({"unused", "rawtypes"}) public class MainActivity extends Activity { ExpandableListView expandableList; TreeViewAdapter adapter; public List<HashMap<String, Object>> categories; @Override 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() { @Override public void onGroupExpand(int groupPosition) { if(groupPosition != 0){ expandableList.setSelectedGroup(groupPosition); } } }); expandableList.setOnChildClickListener(new OnChildClickListener() { @Override 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(); return false; } }); } 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.widget; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.custom.widget.R; import android.content.Context; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; @SuppressWarnings({"rawtypes"}) public class TreeViewAdapter extends BaseExpandableListAdapter { public static final int itemHeight = 140;// 每项的高度 static public class TreeNode { Object parent; List<Object> childs = new ArrayList<Object>(); } List<TreeNode> treeNodes = new ArrayList<TreeNode>(); Context parentContext; public TreeViewAdapter(Context view) { parentContext = view; } public List<TreeNode> GetTreeNode() { return treeNodes; } public void UpdateTreeNode(List<TreeNode> nodes) { treeNodes = nodes; } 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 = null; 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 imageButton; } 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 imageButton; } 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 imageButton; } public long getChildId(int groupPosition, int childPosition) { return childPosition; } public HashMap getGroup(int groupPosition) { return (HashMap) treeNodes.get(groupPosition).parent; } public int getGroupCount() { return treeNodes.size(); } public long getGroupId(int groupPosition) { return groupPosition; } public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } public boolean hasStableIds() { return true; } }
CustomImageButton.java
package cn.android.widget; import org.custom.widget.R; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; public class CustomImageButton extends RelativeLayout { private ImageView imageView; private TextView textView; private TextView textDescription; private ImageView imageViewArrow; 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="http://schemas.android.com/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" /> <TextView 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" /> <TextView 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>
main.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" > <ExpandableListView android:id="@id/category_items" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000"> </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>
ids.xml
<?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>
效果图
下载地址:http://download.csdn.net/detail/wantianwen/7171513