<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ExpandableListView android:id="@+id/province" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#EFEFEF"> </ExpandableListView> </LinearLayout>
package com.example.yanlei.my; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ExpandableListAdapter adapter = new BaseExpandableListAdapter() { //设置组视图的显示文字 private String[] province = new String[] { "河南省", "河北省", "山东省","山西省" }; //子视图显示文字 private String[][] city = new String[][] { { "郑州市", "开封市", "新乡市", "安阳市", "南阳市"}, { "石家庄市", "邯郸市", "保定市", "廊坊市"}, { "济南市", "青岛市", "日照市", "烟台市", "威海市" }, { "太原市", "大同市", "晋城市", "吕梁市", "长治市" } }; //自己定义一个获得文字信息的方法 TextView getTextView() { AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 64); TextView textView = new TextView(MainActivity.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL); textView.setPadding(36, 0, 0, 0); textView.setTextSize(20); textView.setTextColor(Color.BLACK); return textView; } //重写ExpandableListAdapter中的各个方法 @Override public int getGroupCount() { return province.length; } @Override public Object getGroup(int groupPosition) { return province[groupPosition]; } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public int getChildrenCount(int groupPosition) { return city[groupPosition].length; } @Override public Object getChild(int groupPosition, int childPosition) { return city[groupPosition][childPosition]; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(MainActivity.this); ll.setOrientation(0); TextView textView = getTextView(); textView.setTextColor(Color.BLACK); textView.setText(getGroup(groupPosition).toString()); ll.addView(textView); return ll; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(MainActivity.this); ll.setOrientation(0); TextView textView = getTextView(); textView.setText(getChild(groupPosition, childPosition).toString()); ll.addView(textView); return ll; } @Override public boolean isChildSelectable(int groupPosition,int childPosition) { return true; } }; ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.province); expandableListView.setAdapter(adapter); //设置item点击的监听器 expandableListView.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText( MainActivity.this, adapter.getChild(groupPosition, childPosition).toString(), Toast.LENGTH_SHORT).show(); return false; } }); } }