二级菜单的使用
package com.example.day8rikao;
import android.os.Bundle;
import android.app.Activity;
import android.database.DataSetObserver;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ExpandableListView elv;
private String[] types;
private String[][] types2;
private TextView tv_elv;
private TextView text1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件
elv = (ExpandableListView) findViewById(R.id.elv);
//添加要显示的数据
init();
//添加适配器
elv.setAdapter(new MyAdapter());
//添加监听器
elv.setOnChildClickListener(new OnChildClickListener(){
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, "点击!!!",0).show();
return false;
}});
}
class MyAdapter implements ExpandableListAdapter{
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
@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;
}
//返回二级表结构
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
//将布局文件转换为View对象
View view=View.inflate(MainActivity.this,R.layout.activity_item_elv, null);
//查找相关控件
tv_elv = (TextView) view.findViewById(R.id.tv_elv);
//赋值
tv_elv.setText(types2[groupPosition][childPosition]);
return view;
}
//返回二级表个数
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return types2.length;
}
@Override
public long getCombinedChildId(long groupId, long childId) {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getCombinedGroupId(long groupId) {
// TODO Auto-generated method stub
return 0;
}
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return null;
}
//返回一级表个数
public int getGroupCount() {
// TODO Auto-generated method stub
return types.length;
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
//返回一级表结构
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
//将布局文件转换为View对象
View view=View.inflate(MainActivity.this, android.R.layout.simple_list_item_1, null);
//查找相关控件
text1 = (TextView) view.findViewById(android.R.id.text1);
//赋值
text1.setText(types[groupPosition]);
return view;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public void onGroupCollapsed(int groupPosition) {
// TODO Auto-generated method stub
}
@Override
public void onGroupExpanded(int groupPosition) {
// TODO Auto-generated method stub
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
}
private void init() {
types = new String[]{"时尚影音","潮流女包","女装","办公","电脑整机","手机通讯","服饰配件","日用品、玩具"};
types2 = new String[][]{{"耳机","MP3/MP4","数码相框","数码摄影"},
{"手提包","单肩包","双肩包","斜挎包"},
{"连衣裙","半身裙"},
{"办公文具","笔类","财务用品"},
{"笔记本","超极本","平板电脑","台式机"},
{"手机","对讲机"},
{"手表","耳环"},
{"玩具","相框"}};
}
}