二级列表

Adapter

public class MyAdapter extends BaseExpandableListAdapter {
	
	
	private Context context;
	//一级列表数据
	private String[] groups;
	//二级列表数据
	private String[][] childs;
	
	
	public MyAdapter(Context context,String[] groups,String[][] childs){
		this.context=context;
		this.groups=groups;
		this.childs=childs;
	}
	
	
	
	//一级列表数量
	@Override
	public int getGroupCount() {
		return groups.length;
	}

	//当前组中的孩子数
	@Override
	public int getChildrenCount(int groupPosition) {
		return childs[groupPosition].length;
	}

	
	//一级列表数据
	@Override
	public Object getGroup(int groupPosition) {
		return groups[groupPosition];
	}

	//获取当前组当前位置的孩子
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return childs[groupPosition][childPosition];
	}

	
	//一级列表下标
	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	//当前孩子下标
	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	
	//whether or not the same ID always refers to the same object
	@Override
	public boolean hasStableIds() {
		return true;
	}

	//一级列表视图
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		View view = View.inflate(context, R.layout.item_group, null);
		
		TextView groupName=(TextView) view.findViewById(R.id.groupName);
		groupName.setText(groups[groupPosition]);
		
		return view;
	}

	
	//二级列表视图
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		View view = View.inflate(context, R.layout.item_child, null);
		
		TextView childName=(TextView) view.findViewById(R.id.childName);
		childName.setText(childs[groupPosition][childPosition]);
		
		return view;
	}

	
	//该子条目是否可以被选中
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}

MainActivity

public class MainActivity extends Activity {

    private ExpandableListView exListView;
    
  //一级列表数据
  	private String[] groups={"魏国","蜀国","吴国"};
  	//二级列表数据
  	private String[][] childs={
  			{"司马懿","夏侯惇","夏侯淳"},
  			{"诸葛亮","张飞","赵云","关羽"},
  			{"周瑜","黄盖","小乔","孙尚香"}
  	};

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        exListView = (ExpandableListView) findViewById(R.id.exListView);
        
        MyAdapter myAdapter = new MyAdapter(MainActivity.this,groups,childs);
        exListView.setAdapter(myAdapter);
        
        
        //组单击的事件
        exListView.setOnGroupClickListener(new OnGroupClickListener() {
			
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
			Toast.makeText(MainActivity.this, "组单击:"+groupPosition+":"+groups[groupPosition], 0).show();
				return false;
			}
		});
        
        //组折叠的监听
        exListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
			
			@Override
			public void onGroupCollapse(int groupPosition) {
				Toast.makeText(MainActivity.this, "组折叠:"+groupPosition+":"+groups[groupPosition], 0).show();
			}
		});
        
        //可展开的监听
        exListView.setOnGroupExpandListener(new OnGroupExpandListener() {
			
			@Override
			public void onGroupExpand(int groupPosition) {
				Toast.makeText(MainActivity.this, "组展开:"+groupPosition+":"+groups[groupPosition], 0).show();
			}
		});
        
        
        //child单击事件
        exListView.setOnChildClickListener(new OnChildClickListener() {
			
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				Toast.makeText(MainActivity.this, "child单击事件:"+groupPosition+":"+childPosition+childs[groupPosition][childPosition], 0).show();
				return false;
			}
		});
        
        
    }


    
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值