Android中ExpandableListVivew可扩展的ListView(十二)

申思远
2023-12-01

前言:

ExpandableListVivew是ListView的子类,它在普通ListView的基础上进行了扩展,它把应用中的列表项分为几组,每组里 又可包含多个列表项。ExpandableListVivew的用法与普通ListView的用法非常相似,只是ExpandableListVivew 显示的列表项应该由ExpandableAdapter提供。 

实现ExpandableAdapter的三种方式 :

 一、扩展BaseExpandableListAdpter实现ExpandableAdapter。

 二、使用SimpleExpandableListAdpater将两个List集合包装成ExpandableAdapter

 三、使用simpleCursorTreeAdapter将Cursor中的数据包装成SimpleCuroTreeAdapter

package com.itarchy.expandablelistview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
/**
 * 
 * @E-mail: itarchy@163.com
 * @version 创建时间:2015-11-4下午6:39:20
 * @Des:
 */
public class MainActivity extends Activity {

	private ExpandableListView exListView;

	// 数据源
	private String[] groups = { "我的好友", "北京同学", "亲戚", "同事" };
	private String[][] children = {
			{ "李秀婷", "蔡乔", "别高", "余音", "胡算林", "张俊峰", "王志军", "二人", "胡算林", "张俊峰",
					"王志军", "二人" }, { "李秀婷", "蔡乔", "别高", "余音" },
			{ "摊派新", "张爱明" }, { "马超", "司道光" } };

	private MyExpandableAdapter myExAdapter;

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

		exListView = (ExpandableListView) this.findViewById(R.id.exlistview);

		myExAdapter = new MyExpandableAdapter(this, groups, children);

		exListView.setAdapter(myExAdapter);

		exListView.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {

				return false;
			}
		});

	}

}
<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"
     android:background="#ffbbaacc">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="ExpandableListView" />

    <ExpandableListView
        android:id="@+id/exlistview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ExpandableListView>

</RelativeLayout>
package com.itarchy.expandablelistview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyExpandableAdapter extends BaseExpandableListAdapter {

	private Context mContext;
	private String[] groups;
	private String[][] children;

	public MyExpandableAdapter(Context context, String[] groups,
			String[][] children) {
		this.mContext = context;
		this.groups = groups;
		this.children = children;

	}

	// 组的个数
	@Override
	public int getGroupCount() {

		return groups.length;
	}

	// 根据组的位置,组的成员个数
	@Override
	public int getChildrenCount(int groupPosition) {
		// 根据groupPosition获取某一个组的长度
		return groups[groupPosition].length();
	}

	@Override
	public Object getGroup(int groupPosition) {

		return null;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {

		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {

		return 0;
	}

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

		return 0;
	}

	@Override
	public boolean hasStableIds() {

		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		GpViewHolder gpViewHolder = null;
		if (convertView == null) {
			convertView = View.inflate(mContext, R.layout.group_items, null);

			gpViewHolder = new GpViewHolder();
			gpViewHolder.img = (ImageView) convertView.findViewById(R.id.img);
			gpViewHolder.title = (TextView) convertView
					.findViewById(R.id.title);
			convertView.setTag(gpViewHolder);
		} else {
			gpViewHolder = (GpViewHolder) convertView.getTag();
		}
		gpViewHolder.img.setImageResource(R.drawable.qq);
		gpViewHolder.title.setText(groups[groupPosition].toString());
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		GpViewHolder gpViewHolder = null;
		if (convertView == null) {
			convertView = View.inflate(mContext, R.layout.child_items, null);
			gpViewHolder = new GpViewHolder();
			gpViewHolder.img = (ImageView) convertView
					.findViewById(R.id.child_img);
			gpViewHolder.title = (TextView) convertView
					.findViewById(R.id.child_title);
			convertView.setTag(gpViewHolder);
		} else {
			gpViewHolder = (GpViewHolder) convertView.getTag();
		}
		gpViewHolder.img.setImageResource(R.drawable.qq_kong);
		gpViewHolder.title.setText(children[groupPosition][childPosition]
				.toString());
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {

		return false;
	}

	static class GpViewHolder {
		ImageView img;
		TextView title;
	}

}

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_centerVertical="true" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dip"
        android:layout_toRightOf="@+id/img"
        android:textColor="#FFFF00"
        android:textSize="25sp"
        android:textStyle="italic" />

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itarchy.expandablelistview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itarchy.expandablelistview.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>




 类似资料: