设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 249|回复: 0

Android ExpandableList 可折叠列表视图例程

[复制链接]
发表于 2014-5-16 00:14:29 | 显示全部楼层 |阅读模式
在Android开发的过程中,大家希望能实现可折叠列表视图,其实很简单,本例程就是讲解动态创建可折叠列表视图。由于本地系统是日文系统。Eclispse中写注释乱码。注释就用英文写的,只是个简单说明,我想大家能看懂。就是英文太乱了,别见怪啊。
首先 自己创建工程,默认的各XML文件无需修改。

其次 Java 的代码 要继承 ExpandableListActivity

AndroidEggExpandableListActivity.java

  1. /**
  2. * www.androidegg.com
  3. */
  4. package androidegg.com.study.ExpandableList;

  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;

  9. import android.app.ExpandableListActivity;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.View;
  13. import android.widget.ExpandableListAdapter;
  14. import android.widget.ExpandableListView;
  15. import android.widget.SimpleExpandableListAdapter;

  16. public class AndroidEggExpandableListActivity extends ExpandableListActivity {
  17.     /** Called when the activity is first created. */
  18.     @Override
  19.     public void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);

  21.         // parent list
  22.         List<Map<String, Object>> parentList = new ArrayList<Map<String,Object>>();

  23.         // child list
  24.         //
  25.         List<List<Map<String, Object>>> allChildList = new ArrayList<List<Map<String,Object>>>();

  26.         // create 3 parent
  27.         for(int i = 0; i < 3; i++){
  28.             // Map of parent
  29.            
  30.             Map<String, Object> parentData = new HashMap<String, Object>();

  31.             // create parent's title
  32.             parentData.put("TITLE", "Parent" + i);

  33.             // set Map list
  34.             parentList.add(parentData);

  35.             // every parent create 5 child
  36.             List<Map<String, Object>> childList = new ArrayList<Map<String,Object>>();
  37.             for(int j = 0; j < 5; j++){
  38.                 // create child's title
  39.                 Map<String, Object> childData = new HashMap<String, Object>();
  40.                 childData.put("TITLE", "Second" + j);
  41.                 childData.put("SUMMARY", "summary" + j);
  42.                 childList.add(childData);
  43.             }

  44.             // all child set
  45.             allChildList.add(childList);
  46.         }

  47.         // create adapter
  48.         SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
  49.                 this,
  50.                 parentList,
  51.                 android.R.layout.simple_expandable_list_item_1,
  52.                 new String []{"TITLE"},
  53.                 new int []{android.R.id.text1},
  54.                 allChildList,
  55.                 android.R.layout.simple_expandable_list_item_2,
  56.                 new String []{"TITLE", "SUMMARY"},
  57.                 new int []{android.R.id.text1, android.R.id.text2}
  58.         );

  59.         // Adapter set
  60.         setListAdapter(adapter);

  61.         // create child's OnChildClickListener
  62.         ExpandableListView listView = getExpandableListView();
  63.         listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener(){

  64.             /** child click method */
  65.             @SuppressWarnings("unchecked")
  66.             @Override
  67.             public boolean onChildClick(
  68.                     ExpandableListView parent,
  69.                     View v,
  70.                     int groupPosition,
  71.                     int childPosition,
  72.                     long id)
  73.             {
  74.                 // get parent data
  75.                 ExpandableListAdapter adapter = parent.getExpandableListAdapter();

  76.                 // get child data
  77.                 Map<String, Object> childMap = (Map<String, Object>)adapter.getChild(
  78.                         groupPosition,
  79.                         childPosition
  80.                 );

  81.                 // output log
  82.                 Log.d("SampleActivity", "TITLE: " + childMap.get("TITLE"));
  83.                 Log.d("SampleActivity", "SUMMARY: " + childMap.get("SUMMARY"));

  84.                 return false;
  85.             }
  86.         });
  87.     }
  88. }
复制代码

运行结果如下



二,今天给大家讲一下自己设计ExpandableListAdapter,这样大家就可以充分的发挥想象力设计你想要的布局了,直接贴代码吧。
  1. package com.wistron.expandablelist;

  2. import java.util.List;
  3. import java.util.Map;

  4. import android.content.Context;
  5. import android.graphics.Color;
  6. import android.view.Gravity;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.BaseExpandableListAdapter;
  10. import android.widget.ImageView;
  11. import android.widget.LinearLayout;
  12. import android.widget.TextView;

  13. public class ListAdapter extends BaseExpandableListAdapter {
  14. private Context context;
  15. private List<Map<String, Object>> parentList;
  16. private List<List<Map<String, Object>>> allChildList;
  17. public ListAdapter(Context context,
  18.    List<Map<String, Object>> parentList,
  19.    List<List<Map<String, Object>>> allChildList) {
  20.   // TODO Auto-generated constructor stub
  21.   this.context=context;
  22.   this.parentList=parentList;
  23.   this.allChildList=allChildList;
  24. }

  25. @Override
  26. public Object getChild(int groupPosition, int childPosition) {
  27.   // TODO Auto-generated method stub
  28.   return allChildList.get(groupPosition).get(childPosition);
  29. }

  30. @Override
  31. public long getChildId(int groupPosition, int childPosition) {
  32.   // TODO Auto-generated method stub
  33.   return childPosition;
  34. }

  35. @Override
  36. public View getChildView(int groupPosition, int childPosition,
  37.    boolean isLastChild, View convertView, ViewGroup parent) {
  38.   // TODO Auto-generated method stub
  39.   LinearLayout layout=new LinearLayout(context);
  40.   layout.setOrientation(LinearLayout.VERTICAL);
  41.   LinearLayout subLayout1=new LinearLayout(context);
  42.   LinearLayout subLayout2=new LinearLayout(context);
  43.   subLayout1.setOrientation(LinearLayout.HORIZONTAL);
  44.   subLayout2.setOrientation(LinearLayout.HORIZONTAL);
  45.   TextView text1=new TextView(context);
  46.   TextView text2=new TextView(context);
  47.   TextView dot1=new TextView(context);
  48.   TextView dot2=new TextView(context);
  49.   text1.setText(allChildList.get(groupPosition).get(childPosition).get("TITLE").toString());
  50.   text2.setText(allChildList.get(groupPosition).get(childPosition).get("SUMMARY").toString());
  51.   dot1.setTextSize(20);
  52.   dot1.setGravity(Gravity.CENTER);
  53.   dot1.setText(".");
  54.   dot2.setTextSize(20);
  55.   dot2.setGravity(Gravity.CENTER);
  56.   dot2.setText(".");
  57.   subLayout1.addView(dot1);
  58.   subLayout1.addView(text1);
  59.   subLayout2.addView(dot2);
  60.   subLayout2.addView(text2);
  61.   layout.addView(subLayout1);
  62.   layout.addView(subLayout2);
  63.   return layout;
  64. }

  65. @Override
  66. public int getChildrenCount(int groupPosition) {
  67.   // TODO Auto-generated method stub
  68.   return allChildList.size();
  69. }

  70. @Override
  71. public Object getGroup(int groupPosition) {
  72.   // TODO Auto-generated method stub
  73.   return parentList.get(groupPosition);
  74. }

  75. @Override
  76. public int getGroupCount() {
  77.   // TODO Auto-generated method stub
  78.   return parentList.size();
  79. }

  80. @Override
  81. public long getGroupId(int groupPosition) {
  82.   // TODO Auto-generated method stub
  83.   return groupPosition;
  84. }

  85. @Override
  86. public View getGroupView(int groupPosition, boolean isExpanded,
  87.    View convertView, ViewGroup parent) {
  88.   // TODO Auto-generated method stub
  89.   LinearLayout layout=new LinearLayout(context);
  90.   layout.setBackgroundColor(Color.LTGRAY);
  91.   ImageView mImage=new ImageView(context);
  92.   TextView title=new TextView(context);
  93.   mImage.setImageResource(R.drawable.icon);
  94.   title.setText(parentList.get(groupPosition).get("TITLE").toString());
  95.   layout.addView(mImage);
  96.   layout.addView(title);
  97.   return layout;
  98. }

  99. @Override
  100. public boolean hasStableIds() {
  101.   // TODO Auto-generated method stub
  102.   return false;
  103. }

  104. @Override
  105. public boolean isChildSelectable(int groupPosition, int childPosition) {
  106.   // TODO Auto-generated method stub
  107.   return false;
  108. }

  109. }

  110. 2.使用。

  111. ListAdapter adapter=new ListAdapter(this,parentList,allChildList);
  112. setListAdapter(adapter);
复制代码


对于用过适配器的朋友来说应该不难理解,好了,看效果图吧,不懂的可留言给我。
3.效果图



您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-7-8 11:03 , Processed in 0.020172 second(s), 20 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表