1. 程式人生 > >UI_二級列表優化

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

}