UI_二级列表优化

package com.example.second_listview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {
	private ExpandableListView expandableListView;
	private String[] groups;
	private String[][] childs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	
        //传值
        expandableListView.setAdapter(new MyExpandableListView(MainActivity.this,groups,childs));
    	//子元素点击事件
        expandableListView.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				String str = childs[groupPosition][childPosition];
				Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
				return false;
			}
		});
    	
        //父元素点击事件
        expandableListView.setOnGroupClickListener(new OnGroupClickListener() {
			
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				Toast.makeText(MainActivity.this, groups[groupPosition], 0).show();
				return false;
			}
		});
        
	}

	private void initView() {
		groups = new String[]{ "A", "B", "C" };
		childs = new String[][]{ { "A1", "A2", "A3", "A4" },
				{ "A1", "A2", "A3", "B4" }, { "A1", "A2", "A3", "C4" } };
        expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);		
	}
}


//myadpater页面

package com.example.second_listview;
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 MyExpandableListView extends BaseExpandableListAdapter {
	private Context context;
	private String[] groups;
	private String[][] childs;
	public MyExpandableListView(Context context, String[] groups, String[][] childs) {
		this.context = context;
		this.groups = groups;
		this.childs = childs;
	}
	private GroupHolder groupHolder;
	private ChildHolder childHolder;

	// 返回二级列表中的单个item(返回的是对象)
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return null;
	}

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

	// 二级列表
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		groupHolder = new GroupHolder();
		if (convertView== null) {
			convertView = View.inflate(context, R.layout.item_child, null);
			groupHolder.name = (TextView) convertView.findViewById(R.id.tv_child);
			ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_child);
			convertView.setTag(groupHolder);
		}else {
			groupHolder = (GroupHolder) convertView.getTag();
		}
		groupHolder.name.setText(childs[groupPosition][childPosition]);
		return convertView;
	}

	// 子元素的长度
	@Override
	public int getChildrenCount(int groupPosition) {

		return childs[groupPosition].length;

	}
	// 父元素的个数

		@Override
		public int getGroupCount() {
			return groups.length;
		}

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

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

	// 一级列表
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		childHolder = new ChildHolder();
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.item_group, null);
			childHolder.name = (TextView) convertView.findViewById(R.id.tv_group);
			convertView.setTag(childHolder);
		}else {
			childHolder = (ChildHolder) convertView.getTag();
		}
		
		childHolder.name.setText(groups[groupPosition]);
		return convertView;
	}

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

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

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值