欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

QQ 多级列表的实现,qq列表

来源: 开发者 投稿于  被查看 3732 次 评论:150

QQ 多级列表的实现,qq列表


最近因为要去学习IOS的开发了,所以没有太多时间写Android的东西了,不过还是会尽力去多些一点 。
学习Android的人可以交流,Android专业开发群1:150086842 Android专业开发群2:219277004 大家一起加油

1.[图片] 未命名.jpg

2.主类

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

3.自定义的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 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;
	}

}
	

4.主布局文件

<?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>

5.parentList布局

<?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>

6.childList布局

<?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>

用户评论