public class MainActivity extends Activity {
// 定义图片数组
private int[] img = { R.drawable.bai, R.drawable.hua, R.drawable.huang,
R.drawable.hui };
// 定义一级导航
String[] armTyp = { "女装", "男装", "箱包", "配饰" };
// 定义二级导航
String[][] arm = { { "外套", "连衣裙" }, { "皮鞋", "衬衫" }, { "斜跨包", "皮箱" },
{ "耳环", "项链" } };
String[] goods = { "商品1", "商品2", "商品3" };
String[] piyi = { "皮衣", "毛呢", "针织" };
String[] lianyiqun = { "A字裙", "长裙", "沙滩裙" };
String[] pixie = { "真皮鞋", "运动鞋", "帆布鞋" };
String[] chenshan = { "蓝衬衫", "白衬衫", "花衬衫" };
String[] xiekuabao = { "双肩背包", "斜挎包", "背包" };
String[] pixiang = { "大皮箱", "小皮箱", "行李箱" };
String[] erhuan = { "耳钉", "耳棒", "耳环" };
String[] xianglian = { "情侣项链", "单身项链", "心形项链" };
private ExpandableListView expand;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// listview的控件
lv = (ListView) findViewById(R.id.listview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_list_item_1, goods);
lv.setAdapter(adapter);
// ExpandableListView的控件
expand = (ExpandableListView) findViewById(R.id.expand);
// 二级导航的点击事件
expand.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
if (arm[groupPosition][childPosition].equals("外套")) {
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, piyi));
} else if (arm[groupPosition][childPosition].equals("连衣裙")) {
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, lianyiqun));
} else if (arm[groupPosition][childPosition].equals("皮鞋")) {
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, pixie));
} else if (arm[groupPosition][childPosition].equals("衬衫")) {
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, chenshan));
} else if (arm[groupPosition][childPosition].equals("斜跨包")) {
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, xiekuabao));
} else if (arm[groupPosition][childPosition].equals("皮箱")) {
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, pixiang));
} else if (arm[groupPosition][childPosition].equals("耳环")) {
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, erhuan));
} else if (arm[groupPosition][childPosition].equals("项链")) {
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, xianglian));
}
return false;
}
});
expand.setAdapter(new ExpandableListAdapter() {
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
}
@Override
public void onGroupExpanded(int groupPosition) {
}
@Override
public void onGroupCollapsed(int groupPosition) {
}
@Override
public boolean isEmpty() {
return false;
}
// 子列表是否可以 被选中
@Override
public boolean isChildSelectable(int groupPosition,
int childPosition) {
return true;
}
@Override
public boolean hasStableIds() {
return false;
}
// 一级导航的布局
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
convertView = View.inflate(MainActivity.this,
R.layout.activity_group, null);
ImageView group_image = (ImageView) convertView
.findViewById(R.id.group_image);
TextView group_tv = (TextView) convertView
.findViewById(R.id.group_tv);
group_image.setImageResource(img[groupPosition]);
group_tv.setText(armTyp[groupPosition]);
return convertView;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
// 一级导航的个数
@Override
public int getGroupCount() {
return armTyp.length;
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public long getCombinedGroupId(long groupId) {
return 0;
}
@Override
public long getCombinedChildId(long groupId, long childId) {
return 0;
}
@Override
public int getChildrenCount(int groupPosition) {
return arm[groupPosition].length;
}
// 二级导航的布局
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
convertView = View.inflate(MainActivity.this,
R.layout.activity_children, null);
ImageView children_image = (ImageView) convertView
.findViewById(R.id.children_image);
TextView children_tv = (TextView) convertView
.findViewById(R.id.children_tv);
children_image.setImageResource(R.drawable.ic_launcher);
children_tv.setText(arm[groupPosition][childPosition]);
return convertView;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
});
}
// 定义图片数组
private int[] img = { R.drawable.bai, R.drawable.hua, R.drawable.huang,
R.drawable.hui };
// 定义一级导航
String[] armTyp = { "女装", "男装", "箱包", "配饰" };
// 定义二级导航
String[][] arm = { { "外套", "连衣裙" }, { "皮鞋", "衬衫" }, { "斜跨包", "皮箱" },
{ "耳环", "项链" } };
String[] goods = { "商品1", "商品2", "商品3" };
String[] piyi = { "皮衣", "毛呢", "针织" };
String[] lianyiqun = { "A字裙", "长裙", "沙滩裙" };
String[] pixie = { "真皮鞋", "运动鞋", "帆布鞋" };
String[] chenshan = { "蓝衬衫", "白衬衫", "花衬衫" };
String[] xiekuabao = { "双肩背包", "斜挎包", "背包" };
String[] pixiang = { "大皮箱", "小皮箱", "行李箱" };
String[] erhuan = { "耳钉", "耳棒", "耳环" };
String[] xianglian = { "情侣项链", "单身项链", "心形项链" };
private ExpandableListView expand;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// listview的控件
lv = (ListView) findViewById(R.id.listview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this, android.R.layout.simple_list_item_1, goods);
lv.setAdapter(adapter);
// ExpandableListView的控件
expand = (ExpandableListView) findViewById(R.id.expand);
// 二级导航的点击事件
expand.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
if (arm[groupPosition][childPosition].equals("外套")) {
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, piyi));
} else if (arm[groupPosition][childPosition].equals("连衣裙")) {
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, lianyiqun));
} else if (arm[groupPosition][childPosition].equals("皮鞋")) {
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, pixie));
} else if (arm[groupPosition][childPosition].equals("衬衫")) {
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, chenshan));
} else if (arm[groupPosition][childPosition].equals("斜跨包")) {
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, xiekuabao));
} else if (arm[groupPosition][childPosition].equals("皮箱")) {
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, pixiang));
} else if (arm[groupPosition][childPosition].equals("耳环")) {
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, erhuan));
} else if (arm[groupPosition][childPosition].equals("项链")) {
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, xianglian));
}
return false;
}
});
expand.setAdapter(new ExpandableListAdapter() {
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
}
@Override
public void onGroupExpanded(int groupPosition) {
}
@Override
public void onGroupCollapsed(int groupPosition) {
}
@Override
public boolean isEmpty() {
return false;
}
// 子列表是否可以 被选中
@Override
public boolean isChildSelectable(int groupPosition,
int childPosition) {
return true;
}
@Override
public boolean hasStableIds() {
return false;
}
// 一级导航的布局
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
convertView = View.inflate(MainActivity.this,
R.layout.activity_group, null);
ImageView group_image = (ImageView) convertView
.findViewById(R.id.group_image);
TextView group_tv = (TextView) convertView
.findViewById(R.id.group_tv);
group_image.setImageResource(img[groupPosition]);
group_tv.setText(armTyp[groupPosition]);
return convertView;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
// 一级导航的个数
@Override
public int getGroupCount() {
return armTyp.length;
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public long getCombinedGroupId(long groupId) {
return 0;
}
@Override
public long getCombinedChildId(long groupId, long childId) {
return 0;
}
@Override
public int getChildrenCount(int groupPosition) {
return arm[groupPosition].length;
}
// 二级导航的布局
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
convertView = View.inflate(MainActivity.this,
R.layout.activity_children, null);
ImageView children_image = (ImageView) convertView
.findViewById(R.id.children_image);
TextView children_tv = (TextView) convertView
.findViewById(R.id.children_tv);
children_image.setImageResource(R.drawable.ic_launcher);
children_tv.setText(arm[groupPosition][childPosition]);
return convertView;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
});
}