可展开的列表组件ExpandableListView

1、ExpandableListView作为ListView的子类,可将应用中的列表项分为几组,每组里又可以包含多个列表项。其用法与ListView类似,但是,其所显示的额列表项须有ExpandableListAdapter提供。


2、与Adapter类似,ExpandableListAdapter也有三种实现方式:

  • BaseExpandableListAdapter
  • SimpleCursorTreeAdapter
  • SimpleExpandableListAdaptr
3、示例源码
package com.example.expandablelistviewtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ExpandableListAdapter expandableListAdapter=new BaseExpandableListAdapter(){
			
			int[] logos=new int[]{R.drawable.p,R.drawable.t,R.drawable.z};
			private String[] armTypes = new String[]
					{ "神族兵种", "虫族兵种", "人族兵种"};
			private String[][] arms = new String[][]
					{
						{ "狂战士", "龙骑士", "黑暗圣堂", "电兵" },
						{ "小狗", "刺蛇", "飞龙", "自爆飞机" },
						{ "机枪兵", "护士MM" , "幽灵" }
					};
				
			@Override
			public int getGroupCount() {
				// TODO Auto-generated method stub
				return armTypes.length;
			}

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

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

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

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

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

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

			@Override
			public View getGroupView(int groupPosition, boolean isExpanded,
					View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				LinearLayout linearLayout=new LinearLayout(MainActivity.this);
				linearLayout.setOrientation(0);
				ImageView imageView=new ImageView(MainActivity.this);
				imageView.setImageResource(logos[groupPosition]);
				TextView textView=new TextView(MainActivity.this);
				textView.setText(armTypes[groupPosition].toString());
				linearLayout.addView(imageView);
				linearLayout.addView(textView);
				return linearLayout;
			}

			@Override
			public View getChildView(int groupPosition, int childPosition,
					boolean isLastChild, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub
				LinearLayout linearLayout=new LinearLayout(MainActivity.this);
				linearLayout.setOrientation(1);
				LinearLayout linearLayout1=new LinearLayout(MainActivity.this);
				linearLayout.setOrientation(0);
				TextView textView=new TextView(MainActivity.this);
				textView.setText(getChild(groupPosition,childPosition).toString());
				linearLayout1.addView(textView);
				linearLayout.addView(linearLayout1);
				return linearLayout;
			}

			@Override
			public boolean isChildSelectable(int groupPosition,
					int childPosition) {
				// TODO Auto-generated method stub
				return false;
			}
		};
		ExpandableListView expandableListView=(ExpandableListView)findViewById(R.id.expandableListView);
		expandableListView.setAdapter(expandableListAdapter);
	}
}

4、在采用BaseExpandableListAdapter实现ExpandableListAdapter时,需要实现它的一些方法:
  • getGroupConut():返回值为所包含的组列表项的数量;
  • getGroupView():该方法返回的View对象将作为组列表项:
  • getChildrenCount():该方法返回特定组所包含的自列表项的数量;
  • getChildView():该方法返回的View将作为特定组,特定位置的自列表项;
5、实现完ExpandableListAdapter后,ExpandableListAdapter调用setAdapter(adapter)即可。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值