ExpandableListView使用

<LinearLayout 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/expandableListView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:cacheColorHint="#000000"
        android:listSelector="#000000" >
    </ExpandableListView>

</LinearLayout>

Java代码:
package com.gmail.winstone.expandablelistviewtest;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	ExpandableListView listView;

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

		final ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
			// 组视图文字
			private String[] countryStr = { "魏", "蜀", "吴", "群" };
			// 组视图图片
			private int[] countryImg = { R.drawable.wei, R.drawable.shu,
					R.drawable.wu, R.drawable.qun };

			// 子视图文字
			private String[][] nameStr = {
					{ "曹操", "曹植", "曹丕", "郭嘉", "司马懿", "荀攸", "荀彧", "王异" },
					{ "法正", "关羽", "黄月英", "黄忠", "姜维", "刘禅", "孙尚香" },
					{ "步练师", "大乔", "甘宁", "凌统", "陆逊", "孙策", "孙坚", "太史慈" },
					{ "蔡文姬", "陈宫", "SP貂蝉", "高顺", "贾诩", "SP赵云", "左慈" } };

			// 子视图图片
			private int[][] nameImg = {
					{ R.drawable.caocao, R.drawable.caozhi, R.drawable.caopi,
							R.drawable.guojia, R.drawable.simayi,
							R.drawable.xunyou, R.drawable.xunyu,
							R.drawable.wangyi },
					{ R.drawable.fazheng, R.drawable.guanyu,
							R.drawable.huangyueying, R.drawable.huangzhong,
							R.drawable.jiangwei, R.drawable.liushan,
							R.drawable.sunshagnxiang },
					{ R.drawable.bulianshi, R.drawable.daqiao,
							R.drawable.ganning, R.drawable.lingtong,
							R.drawable.luxun, R.drawable.sunce,
							R.drawable.sunjian, R.drawable.taishici },
					{ R.drawable.caiwenji, R.drawable.chengong,
							R.drawable.diaochan, R.drawable.gaoshun,
							R.drawable.jiaxu, R.drawable.zhaoyun,
							R.drawable.zuoci } };

			private TextView getTextView() {
				AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
						ViewGroup.LayoutParams.MATCH_PARENT, 64);
				TextView textView = new TextView(MainActivity.this);
				textView.setLayoutParams(lp);
				textView.setGravity(Gravity.CENTER_VERTICAL);
				textView.setPadding(36, 0, 0, 0);
				textView.setTextSize(20);
				textView.setTextColor(Color.BLACK);
				return textView;
			}

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

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

			@Override
			public View getGroupView(int groupPosition, boolean isExpanded,
					View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				LinearLayout groupLayout = new LinearLayout(MainActivity.this);
				groupLayout.setOrientation(0);
				ImageView logo = new ImageView(MainActivity.this);
				// logo.setLayoutParams(LayoutParams.)
				logo.setImageResource(countryImg[groupPosition]);
				logo.setPadding(50, 0, 0, 0);
				groupLayout.addView(logo);
				TextView tv = getTextView();
				tv.setTextColor(Color.BLACK);
				tv.setText(countryStr[groupPosition].toString());
				groupLayout.addView(tv);
				return groupLayout;
			}

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

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

			@Override
			public Object getGroup(int groupPosition) {
				// TODO Auto-generated method stub
				return countryStr[groupPosition];
			}

			@Override
			public int getChildrenCount(int groupPosition) {
				// TODO Auto-generated method stub
				return nameStr[groupPosition].length;
			}

			@Override
			public View getChildView(int groupPosition, int childPosition,
					boolean isLastChild, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				LinearLayout ll = new LinearLayout(MainActivity.this);
				ll.setOrientation(0);
				ImageView iv = new ImageView(MainActivity.this);
				iv.setImageResource(nameImg[groupPosition][childPosition]);
				ll.addView(iv);
				TextView tv = getTextView();
				tv.setTextColor(Color.CYAN);
				tv.setText(nameStr[groupPosition][childPosition]);
				ll.addView(tv);
				return ll;
			}

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

			@Override
			public Object getChild(int groupPosition, int childPosition) {
				// TODO Auto-generated method stub
				return nameStr[groupPosition][childPosition];
			}
		};

		listView = (ExpandableListView) findViewById(R.id.expandableListView1);
		listView.setAdapter(adapter);
		
		listView.setOnChildClickListener(new OnChildClickListener() {
			
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TODO Auto-generated method stub
				Toast.makeText(
                        MainActivity.this,
						"你点击了" + adapter.getChild(groupPosition, childPosition),
                        Toast.LENGTH_SHORT).show();
				return false;
			}
		});

	}

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

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值