在Android开发的过程中,大家希望能实现可折叠列表视图,其实很简单,本例程就是讲解动态创建可折叠列表视图。由于本地系统是日文系统。Eclispse中写注释乱码。注释就用英文写的,只是个简单说明,我想大家能看懂。就是英文太乱了,别见怪啊。
首先 自己创建工程,默认的各XML文件无需修改。
其次 Java 的代码 要继承 ExpandableListActivity
AndroidEggExpandableListActivity.java
- /**
- * www.androidegg.com
- */
- package androidegg.com.study.ExpandableList;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.app.ExpandableListActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.ExpandableListAdapter;
- import android.widget.ExpandableListView;
- import android.widget.SimpleExpandableListAdapter;
- public class AndroidEggExpandableListActivity extends ExpandableListActivity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // parent list
- List<Map<String, Object>> parentList = new ArrayList<Map<String,Object>>();
- // child list
- //
- List<List<Map<String, Object>>> allChildList = new ArrayList<List<Map<String,Object>>>();
- // create 3 parent
- for(int i = 0; i < 3; i++){
- // Map of parent
-
- Map<String, Object> parentData = new HashMap<String, Object>();
- // create parent's title
- parentData.put("TITLE", "Parent" + i);
- // set Map list
- parentList.add(parentData);
- // every parent create 5 child
- List<Map<String, Object>> childList = new ArrayList<Map<String,Object>>();
- for(int j = 0; j < 5; j++){
- // create child's title
- Map<String, Object> childData = new HashMap<String, Object>();
- childData.put("TITLE", "Second" + j);
- childData.put("SUMMARY", "summary" + j);
- childList.add(childData);
- }
- // all child set
- allChildList.add(childList);
- }
- // create adapter
- SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
- this,
- parentList,
- android.R.layout.simple_expandable_list_item_1,
- new String []{"TITLE"},
- new int []{android.R.id.text1},
- allChildList,
- android.R.layout.simple_expandable_list_item_2,
- new String []{"TITLE", "SUMMARY"},
- new int []{android.R.id.text1, android.R.id.text2}
- );
- // Adapter set
- setListAdapter(adapter);
- // create child's OnChildClickListener
- ExpandableListView listView = getExpandableListView();
- listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener(){
- /** child click method */
- @SuppressWarnings("unchecked")
- @Override
- public boolean onChildClick(
- ExpandableListView parent,
- View v,
- int groupPosition,
- int childPosition,
- long id)
- {
- // get parent data
- ExpandableListAdapter adapter = parent.getExpandableListAdapter();
- // get child data
- Map<String, Object> childMap = (Map<String, Object>)adapter.getChild(
- groupPosition,
- childPosition
- );
- // output log
- Log.d("SampleActivity", "TITLE: " + childMap.get("TITLE"));
- Log.d("SampleActivity", "SUMMARY: " + childMap.get("SUMMARY"));
- return false;
- }
- });
- }
- }
复制代码
运行结果如下

二,今天给大家讲一下自己设计ExpandableListAdapter,这样大家就可以充分的发挥想象力设计你想要的布局了,直接贴代码吧。- package com.wistron.expandablelist;
- import java.util.List;
- import java.util.Map;
- import android.content.Context;
- import android.graphics.Color;
- import android.view.Gravity;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseExpandableListAdapter;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- public class ListAdapter extends BaseExpandableListAdapter {
- private Context context;
- private List<Map<String, Object>> parentList;
- private List<List<Map<String, Object>>> allChildList;
- public ListAdapter(Context context,
- List<Map<String, Object>> parentList,
- List<List<Map<String, Object>>> allChildList) {
- // TODO Auto-generated constructor stub
- this.context=context;
- this.parentList=parentList;
- this.allChildList=allChildList;
- }
- @Override
- public Object getChild(int groupPosition, int childPosition) {
- // TODO Auto-generated method stub
- return allChildList.get(groupPosition).get(childPosition);
- }
- @Override
- public long getChildId(int groupPosition, int childPosition) {
- // TODO Auto-generated method stub
- return childPosition;
- }
- @Override
- public View getChildView(int groupPosition, int childPosition,
- boolean isLastChild, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- LinearLayout layout=new LinearLayout(context);
- layout.setOrientation(LinearLayout.VERTICAL);
- LinearLayout subLayout1=new LinearLayout(context);
- LinearLayout subLayout2=new LinearLayout(context);
- subLayout1.setOrientation(LinearLayout.HORIZONTAL);
- subLayout2.setOrientation(LinearLayout.HORIZONTAL);
- TextView text1=new TextView(context);
- TextView text2=new TextView(context);
- TextView dot1=new TextView(context);
- TextView dot2=new TextView(context);
- text1.setText(allChildList.get(groupPosition).get(childPosition).get("TITLE").toString());
- text2.setText(allChildList.get(groupPosition).get(childPosition).get("SUMMARY").toString());
- dot1.setTextSize(20);
- dot1.setGravity(Gravity.CENTER);
- dot1.setText(".");
- dot2.setTextSize(20);
- dot2.setGravity(Gravity.CENTER);
- dot2.setText(".");
- subLayout1.addView(dot1);
- subLayout1.addView(text1);
- subLayout2.addView(dot2);
- subLayout2.addView(text2);
- layout.addView(subLayout1);
- layout.addView(subLayout2);
- return layout;
- }
- @Override
- public int getChildrenCount(int groupPosition) {
- // TODO Auto-generated method stub
- return allChildList.size();
- }
- @Override
- public Object getGroup(int groupPosition) {
- // TODO Auto-generated method stub
- return parentList.get(groupPosition);
- }
- @Override
- public int getGroupCount() {
- // TODO Auto-generated method stub
- return parentList.size();
- }
- @Override
- public long getGroupId(int groupPosition) {
- // TODO Auto-generated method stub
- return groupPosition;
- }
- @Override
- public View getGroupView(int groupPosition, boolean isExpanded,
- View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- LinearLayout layout=new LinearLayout(context);
- layout.setBackgroundColor(Color.LTGRAY);
- ImageView mImage=new ImageView(context);
- TextView title=new TextView(context);
- mImage.setImageResource(R.drawable.icon);
- title.setText(parentList.get(groupPosition).get("TITLE").toString());
- layout.addView(mImage);
- layout.addView(title);
- return layout;
- }
- @Override
- public boolean hasStableIds() {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public boolean isChildSelectable(int groupPosition, int childPosition) {
- // TODO Auto-generated method stub
- return false;
- }
- }
- 2.使用。
- ListAdapter adapter=new ListAdapter(this,parentList,allChildList);
- setListAdapter(adapter);
复制代码
对于用过适配器的朋友来说应该不难理解,好了,看效果图吧,不懂的可留言给我。
3.效果图

|