自定义Adapter填充ExpandableListView

<span style="font-family:Microsoft YaHei;font-size:12px;"><span style="font-family:Microsoft YaHei;font-size:12px;">activity_main布局:</span></span>
<span style="font-family:Microsoft YaHei;font-size:12px;"><span style="font-family:Microsoft YaHei;font-size:12px;"></span><pre name="code" class="html"><span style="font-family:Microsoft YaHei;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ExpandableListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" >
    </ExpandableListView>

</RelativeLayout>
</span></span>


 

MainActivity

<span style="font-family:Microsoft YaHei;font-size:12px;"><span style="font-family:Microsoft YaHei;font-size:12px;">package com.example.zza_android_test4;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;

public class MainActivity extends Activity {

	/**
	 * childnames:死机姓名 childnamess:次数/里程
	 */
	private String[] names = { "腾讯", "百度", "阿里巴巴" };
	private String[][] childnames = { { "QQ", "微信", "手机卫士" },
			{ "百度地图", "百度视频", "PPS&奇艺" }, { "支付宝", "新郎微博", "高德地图" } };
	private String[][] childnamess = {
			{ "2343次/108公里", "2343次/108公里", "2343次/108公里" },
			{ "2343次/108公里", "2343次/108公里", "2343次/108公里" },
			{ "2343次/108公里", "2343次/108公里", "2343次/108公里" } };

	private List<Map<String, String>> group0;// 头部数据("驾驶员"、"车辆"、"路线")
	private List<List<Map<String, String>>> child0; // {"QQ"、"2343次/108公里"}
													// tencent-->item里的子项数据
	private ExpandableListView list;
	private SimpleExpandableListAdapter adapter;
	private MyAdapter1 adapter1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initData();
		list = (ExpandableListView) findViewById(R.id.list);
		// adapter = new SimpleExpandableListAdapter(this, group,
		// R.layout.headers, new String[] { "names" },
		// new int[] { R.id.text_header }, child, R.layout.items,
		// new String[] { "xingming","licheng" }, new int[] {
		// R.id.text_item,R.id.text_licheng });

		adapter1 = new MyAdapter1(MainActivity.this, group0, child0);
		list.setAdapter(adapter1);

	}

	private void initData() { // 只是"腾讯"头里的一组数据
		group0 = new ArrayList<Map<String, String>>();
		child0 = new ArrayList<List<Map<String, String>>>();
		for (int i = 0; i < names.length; i++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("names", names[i]);
			group0.add(map);

			List<Map<String, String>> item = new ArrayList<Map<String, String>>();
			for (int j = 0; j < names.length; j++) {
				Map<String, String> maps = new HashMap<String, String>();
				maps.put("xingming", childnames[i][j]);
				maps.put("licheng", childnamess[i][j]);
				item.add(maps);
			}
			child0.add(item);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
</span></span>
headers布局:

<span style="font-family:Microsoft YaHei;font-size:12px;"><span style="font-family:Microsoft YaHei;font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- android:textColor="@color/white" -->

    <TextView
        android:id="@+id/text_header"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#F5A623"
        android:gravity="center_vertical"
        android:paddingLeft="30dp"
        android:text="hhF"
        android:textSize="18sp" />

</LinearLayout></span></span>
items布局:

<span style="font-family:Microsoft YaHei;font-size:12px;"><span style="font-family:Microsoft YaHei;font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:id="@+id/childItem_ll"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#EFEFEF" >

        <TextView
            android:id="@+id/text_item"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:paddingLeft="20dp"
            android:gravity="center"
            android:text="hhF" />

        <TextView
            android:id="@+id/text_licheng"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="40dp"
            android:layout_toRightOf="@id/text_item"
            android:gravity="center_vertical|right"
            android:text="hhF" />
    </RelativeLayout>

</RelativeLayout></span></span>
Adapter:

<span style="font-family:Microsoft YaHei;font-size:12px;"><span style="font-family:Microsoft YaHei;font-size:12px;">package com.example.zza_android_test4;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MyAdapter1 extends BaseExpandableListAdapter {

	private Context mContext;
	private LayoutInflater mInflater;
	private List<Map<String, String>> mGroup;
	private List<List<Map<String, String>>> mChild;

	public MyAdapter1(Context mContext, List<Map<String, String>> mGroup,
			List<List<Map<String, String>>> mChild) {
		this.mContext = mContext;
		this.mGroup = mGroup;
		this.mChild = mChild;
		this.mInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return mGroup.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		Log.e("getChildrenCount", mChild.get(groupPosition).size() + "");
		return mChild.get(groupPosition).size();
	}

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

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return mChild.get(groupPosition).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		GroupHolderView holder;
		if (convertView == null) {
			holder = new GroupHolderView();
			convertView = mInflater.inflate(R.layout.headers, null);
			holder.groupTv = (TextView) convertView
					.findViewById(R.id.text_header);
			convertView.setTag(holder);
		} else {
			holder = (GroupHolderView) convertView.getTag();
		}
		holder.groupTv.setText(mGroup.get(groupPosition).get("names"));
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ChildHolderView holder1;
		if (convertView == null) {
			holder1 = new ChildHolderView();
			convertView = mInflater.inflate(R.layout.items, null);
			holder1.childTv = (TextView) convertView
					.findViewById(R.id.text_item);
			holder1.childTvShu = (TextView) convertView
					.findViewById(R.id.text_licheng);
			holder1.childItem_ll = (RelativeLayout) convertView
					.findViewById(R.id.childItem_ll);
			convertView.setTag(holder1);
		} else {
			holder1 = (ChildHolderView) convertView.getTag();
		}
		for(int i=0;i<mChild.size();i++){
			if(i%2==0){
				holder1.childItem_ll.setBackgroundColor(Color.parseColor("#EFEFEF"));
			}else{
				holder1.childItem_ll.setBackgroundColor(Color.parseColor("#FFFFFF"));
			}
		}
		holder1.childTv.setText(mChild.get(groupPosition).get(childPosition)
				.get("xingming"));
		holder1.childTvShu.setText(mChild.get(groupPosition).get(childPosition)
				.get("licheng"));
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	class GroupHolderView {
		TextView groupTv;
	}

	class ChildHolderView {
		TextView childTv;
		TextView childTvShu;
		RelativeLayout childItem_ll;
	}
}
</span></span>

代码





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值