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开发的学习历程,本demo为可拓展的列表控件ExpandableListView的使用方法,并含有要点整理文档。
android学习基础知识,不懂的童鞋看看,一定有收获的~
最近开发项目遇到了一个分组的列表,我以为是第三方的,在网上一搜原来是原生的,所以写了一个例子,上传到这,和大家交流学习
实现二级列表下拉,如listview,自定义适配器,初学者可以学习下
关于ExpandableListView的控件,有个需求就是之展开一条,写了一个例子上传到这里,和大家交流学习
android的ExpandableListView组件.doc
Android应用源码开发Demo,主要用于毕业设计学习。
xamarin android expandableListView一个二级的listview学习实例,使用比较简单和listview其实差不多,二级的listview也是比较常用的控件。
Android学习笔记(二五): 多信息显示-ExpandableListView的使用
Android 基于自定义适配器的ExpandableListView源码.zip项目安卓应用源码下载Android 基于自定义适配器的ExpandableListView源码.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3....
Android应用源码之基于自定义适配器的ExpandableListView.zip项目安卓应用源码下载Android应用源码之基于自定义适配器的ExpandableListView.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究...
ExpandableListView实现购物车页面.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
网上找了很久才找到的,很有帮助,希望大家共同学习,
Android 应用项目开发源码 参考和学习使用
在本Android ExpandablelistView教程中,我们将学习如何在Android中制作可扩展列表。
带全选按钮的ExpandableListView,可实现全选,android stutio项目,可直接运行.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Android应用源码开发Demo,主要用于毕业设计学习。
Android应用源码模仿QQ的扩展型很好的ExpandableListView.zip项目安卓应用源码下载Android应用源码模仿QQ的扩展型很好的ExpandableListView.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究...
Android应用源码开发Demo,主要用于毕业设计学习。
Android应用源码开发Demo,主要用于毕业设计学习。