Android 开发 listview QQ多级列表的实现

发布时间:2013-9-24 17:36    发布者:reggae
关键词: android
[代码] 主类

  1. package com.android.qu.antking.list;

  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseExpandableListAdapter;
  9. import android.widget.ExpandableListView;
  10. import android.widget.ImageView;
  11. import android.widget.TextView;

  12. import java.util.*;



  13. public class MyMain extends Activity {
  14.   //author antkingwei
  15. private List> parentList=new ArrayList>();

  16. private List>> childList = new ArrayList>>();

  17. ExpendAdapter adapter;

  18. ExpandableListView exList;

  19. private String[] listName = new String[]{
  20. "我的好友","高中同学","大学同学","移动开发","网站建设","普通朋友"
  21. };
  22. private String[] childTitle= new String[]{
  23. "丫宁","王八锐","小鸟","连超","董二丫"
  24. };
  25. private String[] childMood= new String[]{
  26. "我喜欢王锐","我就是王八","我也喜欢王锐","上边一群傻帽","同楼上"
  27. };
  28. private int[] headImage=new int[]{
  29. R.drawable.ning,R.drawable.rui,R.drawable.niao,R.drawable.lianchao,R.drawable.xiaoxiao
  30. };

  31.     public void onCreate(Bundle savedInstanceState) {
  32.         super.onCreate(savedInstanceState);
  33.         setContentView(R.layout.main);

  34.         exList = (ExpandableListView) this.findViewById(R.id.expandableListView1);
  35.         parentList =getParentList();
  36.         childList = getChildList();
  37.        adapter = new ExpendAdapter(MyMain.this, parentList, childList);
  38.       
  39.         exList.setAdapter(adapter);
  40.         exList.setGroupIndicator(null);
  41.         exList.setDivider(null);
  42.         
  43.     }
  44.     public List> getParentList(){
  45.     List> list = new ArrayList>();
  46.     for(int i=0;i
  47.     Map curGroupMap = new HashMap();
  48.              list.add(curGroupMap);
  49.              curGroupMap.put("List", listName[i]);
  50.     }
  51.     return list;
  52.     }
  53.     public List>> getChildList(){
  54.     List>> list1 = new ArrayList>>();
  55.     for (int i = 0; i < listName.length; i++) {
  56.             
  57.                
  58.              List> children = new ArrayList>();
  59.              for (int j = 0; j
  60.                  Map curChildMap = new HashMap();
  61.                  children.add(curChildMap);
  62.                  curChildMap.put("Title", childTitle[j]);
  63.                  curChildMap.put("Mood", childMood[j]);
  64.                  curChildMap.put("Head", headImage[j]);
  65.              }
  66.              list1.add(children);
  67.     }
  68.     return list1;
  69.    
  70.     }
  71.    
  72. }
复制代码

[代码] 自定义的Adapter

  1. package com.android.qu.antking.list;

  2. import android.content.Context;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.BaseExpandableListAdapter;
  7. import android.widget.ImageView;
  8. import android.widget.TextView;
  9. import android.widget.Toast;

  10. import java.util.*;

  11. public class ExpendAdapter extends BaseExpandableListAdapter {
  12.       private LayoutInflater layoutInflater;
  13.       
  14.       private Context mContext;
  15.       
  16.       private List> parentList = new ArrayList>();
  17.       
  18.       private List>> childList = new ArrayList>>();
  19.       
  20.       
  21. public ExpendAdapter(Context mContext,List> parentList,List>> childList){


  22. this.mContext = mContext;

  23. this.parentList = parentList;

  24. this.childList = childList;

  25. layoutInflater = LayoutInflater.from(mContext);
  26. }
  27. public Object getChild(int groupPosition, int childPosition) {
  28. // TODO Auto-generated method stub
  29. return childList.get(groupPosition).get(childPosition).get("Title").toString();
  30. }

  31. @Override
  32. public long getChildId(int groupPosition, int childPosition) {
  33. return childPosition;
  34. }

  35. @Override
  36. public View getChildView(int groupPosition, int childPosition,
  37. boolean isLastChild, View convertView, ViewGroup parent) {

  38. if(convertView ==null){
  39. convertView = layoutInflater.inflate(R.layout.childlist, null);

  40. }
  41. final ImageView head=(ImageView)convertView.findViewById(R.id.headImage);
  42.   head.setImageResource(Integer.valueOf(childList.get(groupPosition).get(childPosition).get("Head").toString()));
  43. final TextView title=(TextView)convertView.findViewById(R.id.title);
  44. title.setText(childList.get(groupPosition).get(childPosition).get("Title").toString());

  45.    final TextView mood =(TextView)convertView.findViewById(R.id.mood);
  46.      mood.setText(childList.get(groupPosition).get(childPosition).get("Mood").toString());
  47. return convertView;
  48. }

  49. @Override
  50. public int getChildrenCount(int groupPosition) {
  51. // TODO Auto-generated method stub
  52. return childList.get(groupPosition).size();
  53. }

  54. @Override
  55. public Object getGroup(int groupPosition) {
  56. // TODO Auto-generated method stub
  57. return parentList.get(groupPosition).get("List").toString();
  58. }

  59. @Override
  60. public int getGroupCount() {
  61. // TODO Auto-generated method stub
  62. return parentList.size();
  63. }

  64. @Override
  65. public long getGroupId(int groupPosition) {
  66. // TODO Auto-generated method stub
  67. return groupPosition;
  68. }

  69. @Override
  70. public View getGroupView(int groupPosition, boolean isExpanded,
  71. View convertView, ViewGroup parent) {

  72. if(convertView==null){
  73. convertView=layoutInflater.inflate(R.layout.parentlist, null);

  74. }
  75. final TextView list = (TextView) convertView.findViewById(R.id.list);
  76. list.setText(parentList.get(groupPosition).get("List").toString());
  77. return convertView;
  78. }

  79. @Override
  80. public boolean hasStableIds() {
  81. // TODO Auto-generated method stub
  82. Toast.makeText(mContext,"nihao",Toast.LENGTH_SHORT).show();
  83. return true;
  84. }

  85. @Override
  86. public boolean isChildSelectable(int groupPosition, int childPosition) {
  87. Toast.makeText(mContext, "这是第"+groupPosition+"组,第"+childPosition+"个", Toast.LENGTH_SHORT).show();
  88. return true;
  89. }

  90. }
复制代码

[代码] 主布局文件


  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:orientation="vertical"
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent"
  5.     android:background="@drawable/back1"
  6.     >


复制代码

[代码] parentList布局


  1.   xmlns:android="http://schemas.android.com/apk/res/android"
  2.   android:layout_width="fill_parent"
  3.   android:orientation="horizontal"
  4.   android:id="@+id/parentList"
  5.   android:layout_height="wrap_content">
  6.   
  7.     android:layout_width="60px"
  8.     android:layout_height="60px"
  9.     android:src="@drawable/user_group"
  10.    />
  11.    
  12.     android:id="@+id/list"
  13.     android:textSize="20px"
  14.     android:layout_width="wrap_content"
  15.     android:layout_height="wrap_content"/>
  16.   
复制代码

[代码] childList布局


  1.   xmlns:android="http://schemas.android.com/apk/res/android"
  2.   android:layout_width="fill_parent"
  3.   android:layout_height="wrap_content"
  4.   android:id="@+id/childList"
  5.   android:orientation="horizontal"
  6.   >
  7.   
  8.     android:paddingLeft="20px"
  9.     android:id="@+id/headImage"
  10.     android:src="@drawable/icon"
  11.     android:layout_width="50px"
  12.     android:layout_height="50px"
  13.     android:layout_marginBottom="5px"
  14.     android:layout_marginRight="10px"/>
  15.   
  16.    android:orientation="vertical"
  17.    android:layout_width="fill_parent"
  18.    
  19.    android:layout_height="wrap_content">
  20.    
  21.    android:textSize="18px"
  22.    android:layout_width="wrap_content"
  23.    android:layout_height="wrap_content"
  24.    />
  25.    
  26.    android:textSize="16px"
  27.    android:layout_width="wrap_content"
  28.    android:layout_height="wrap_content"/>
  29.    
  30.    
  31.    
复制代码

希望本文对广大安卓开发者有所帮助,感谢阅读本文。更多安卓技术问题欢迎加群探讨:314230976,验证码:eec,不写验证不予通过哟~

本文地址:https://www.eechina.com/thread-121310-1-1.html     【打印本页】

本站部分文章为转载或网友发布,目的在于传递和分享信息,并不代表本网赞同其观点和对其真实性负责;文章版权归原作者及原出处所有,如涉及作品内容、版权和其它问题,我们将根据著作权人的要求,第一时间更正或删除。
您需要登录后才可以发表评论 登录 | 立即注册

厂商推荐

相关视频

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备16069177号 | 京公网安备11010502021702
快速回复 返回顶部 返回列表