首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android QQ多级列表的兑现

2012-07-20 
Android QQ多级列表的实现?主类:package com.android.qu.antking.listimport android.app.Activityimpor

Android QQ多级列表的实现


Android QQ多级列表的兑现
?主类:

package com.android.qu.antking.list;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.ImageView;import android.widget.TextView;import java.util.*;public class MyMain extends Activity {  //author antkingweiprivate List<Map<String,Object>> parentList=new ArrayList<Map<String,Object>>();private List<List<Map<String,Object>>> childList = new ArrayList<List<Map<String,Object>>>();ExpendAdapter adapter;ExpandableListView exList;private String[] listName = new String[]{"我的好友","高中同学","大学同学","移动开发","网站建设","普通朋友"};private String[] childTitle= new String[]{"丫宁","王八锐","小鸟","连超","董二丫"};private String[] childMood= new String[]{"我喜欢王锐","我就是王八","我也喜欢王锐","上边一群傻帽","同楼上"};private int[] headImage=new int[]{R.drawable.ning,R.drawable.rui,R.drawable.niao,R.drawable.lianchao,R.drawable.xiaoxiao};    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        exList = (ExpandableListView) this.findViewById(R.id.expandableListView1);        parentList =getParentList();        childList = getChildList();       adapter = new ExpendAdapter(MyMain.this, parentList, childList);              exList.setAdapter(adapter);        exList.setGroupIndicator(null);        exList.setDivider(null);            }    public List<Map<String,Object>> getParentList(){    List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();    for(int i=0;i<listName.length;i++){     Map<String, Object> curGroupMap = new HashMap<String, Object>();             list.add(curGroupMap);             curGroupMap.put("List", listName[i]);    }    return list;    }    public List<List<Map<String,Object>>> getChildList(){    List<List<Map<String,Object>>> list1 = new ArrayList<List<Map<String,Object>>>();     for (int i = 0; i < listName.length; i++) {                                         List<Map<String, Object>> children = new ArrayList<Map<String, Object>>();             for (int j = 0; j <childTitle.length; j++) {                 Map<String, Object> curChildMap = new HashMap<String, Object>();                 children.add(curChildMap);                 curChildMap.put("Title", childTitle[j]);                 curChildMap.put("Mood", childMood[j]);                 curChildMap.put("Head", headImage[j]);             }             list1.add(children);    }    return list1;        }   }

?自定义的Adapter

package com.android.qu.antking.list;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.TextView;import android.widget.Toast;import java.util.*;public class ExpendAdapter extends BaseExpandableListAdapter {      private LayoutInflater layoutInflater;            private Context mContext;            private List<Map<String,Object>> parentList = new ArrayList<Map<String,Object>>();            private List<List<Map<String,Object>>> childList = new ArrayList<List<Map<String,Object>>>();            public ExpendAdapter(Context mContext,List<Map<String,Object>> parentList,List<List<Map<String,Object>>> childList){this.mContext = mContext;this.parentList = parentList;this.childList = childList;layoutInflater = LayoutInflater.from(mContext);}public Object getChild(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn childList.get(groupPosition).get(childPosition).get("Title").toString();}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {if(convertView ==null){convertView = layoutInflater.inflate(R.layout.childlist, null);}final ImageView head=(ImageView)convertView.findViewById(R.id.headImage);   head.setImageResource(Integer.valueOf(childList.get(groupPosition).get(childPosition).get("Head").toString()));final TextView title=(TextView)convertView.findViewById(R.id.title);  title.setText(childList.get(groupPosition).get(childPosition).get("Title").toString());      final TextView mood =(TextView)convertView.findViewById(R.id.mood);      mood.setText(childList.get(groupPosition).get(childPosition).get("Mood").toString());return convertView;}@Overridepublic int getChildrenCount(int groupPosition) {// TODO Auto-generated method stubreturn childList.get(groupPosition).size();}@Overridepublic Object getGroup(int groupPosition) {// TODO Auto-generated method stubreturn parentList.get(groupPosition).get("List").toString();}@Overridepublic int getGroupCount() {// TODO Auto-generated method stubreturn parentList.size();}@Overridepublic long getGroupId(int groupPosition) {// TODO Auto-generated method stubreturn groupPosition;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {if(convertView==null){convertView=layoutInflater.inflate(R.layout.parentlist, null);}final TextView list = (TextView) convertView.findViewById(R.id.list);  list.setText(parentList.get(groupPosition).get("List").toString());return convertView;}@Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubToast.makeText(mContext,"nihao",Toast.LENGTH_SHORT).show();return true;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {Toast.makeText(mContext, "这是第"+groupPosition+"组,第"+childPosition+"个", Toast.LENGTH_SHORT).show();return true;}}

?主布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@drawable/back1"    ><ExpandableListView android:layout_height="wrap_content" android:id="@+id/expandableListView1" android:layout_width="fill_parent"></ExpandableListView></LinearLayout>

?另一个布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:orientation="horizontal"  android:id="@+id/parentList"  android:layout_height="wrap_content">  <ImageView     android:layout_width="60px"    android:layout_height="60px"    android:src="@drawable/user_group"   />   <TextView    android:id="@+id/list"    android:textSize="20px"    android:layout_width="wrap_content"    android:layout_height="wrap_content"/>  </LinearLayout>

?最后一段布局

<?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="wrap_content"  android:id="@+id/childList"  android:orientation="horizontal"  >  <ImageView    android:paddingLeft="20px"    android:id="@+id/headImage"    android:src="@drawable/icon"    android:layout_width="50px"    android:layout_height="50px"    android:layout_marginBottom="5px"    android:layout_marginRight="10px"/>  <LinearLayout   android:orientation="vertical"   android:layout_width="fill_parent"      android:layout_height="wrap_content">   <TextView android:id="@+id/title"   android:textSize="18px"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   />   <TextView android:id="@+id/mood"   android:textSize="16px"   android:layout_width="wrap_content"   android:layout_height="wrap_content"/>      </LinearLayout></LinearLayout>
?

热点排行