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

2013年09月24日 17:36    发布者:reggae
[代码] 主类

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 antkingwei
private List> parentList=new ArrayList>();

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

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> getParentList(){
    List> list = new ArrayList>();
    for(int i=0;i     Map curGroupMap = new HashMap();
             list.add(curGroupMap);
             curGroupMap.put("List", listName);
    }
    return list;
    }
    public List>> getChildList(){
    List>> list1 = new ArrayList>>();
    for (int i = 0; i < listName.length; i++) {
            
               
             List> children = new ArrayList>();
             for (int j = 0; j                  Map curChildMap = new HashMap();
                 children.add(curChildMap);
                 curChildMap.put("Title", childTitle);
                 curChildMap.put("Mood", childMood);
                 curChildMap.put("Head", headImage);
             }
             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> parentList = new ArrayList>();
      
      private List>> childList = new ArrayList>>();
      
      
public ExpendAdapter(Context mContext,List> parentList,List>> 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 stub
return childList.get(groupPosition).get(childPosition).get("Title").toString();
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public 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;
}

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

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

@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) {

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;
}

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

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

}
[代码] 主布局文件


    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/back1"
    >



[代码] parentList布局


  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">
       android:layout_width="60px"
    android:layout_height="60px"
    android:src="@drawable/user_group"
   />
        android:id="@+id/list"
    android:textSize="20px"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
  

[代码] childList布局


  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"
  >
       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"/>
      android:orientation="vertical"
   android:layout_width="fill_parent"
   
   android:layout_height="wrap_content">
       android:textSize="18px"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   />
       android:textSize="16px"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"/>
   
   
   
希望本文对广大安卓开发者有所帮助,感谢阅读本文。更多安卓技术问题欢迎加群探讨:314230976,验证码:eec,不写验证不予通过哟~