首页 > 代码库 > expandablelistview 的用法

expandablelistview 的用法

package com.example.getshareperference;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.TextView;public class MainActivity extends Activity {    private ExpandableListView listview;    private MyAdapter myAdapter;    private List<String> group;    private List<List<String>> child;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initData();        listview = (ExpandableListView) findViewById(R.id.expandableListView1);        myAdapter = new MyAdapter();        listview.setAdapter(myAdapter);        listview.setGroupIndicator(null);    }    public void initData() {        group = new ArrayList<String>();        child = new ArrayList<List<String>>();        addInfo("湖北省", new String[] { "武汉市", "黄石市", "荆州市", "随州市", "宜昌市" });        addInfo("湖南省", new String[] { "长沙市", "株洲市", "湘潭市", "衡阳市", "邵阳市" });        addInfo("广东省", new String[] { "广州市", "深圳市", "珠海市", "汕头市", "佛山市" });        addInfo("广西省", new String[] { "南宁市", "柳州市", "桂林市", "梧州市", "北海市" });            }    public void addInfo(String g, String[] c) {        group.add(g);        List<String> list = new ArrayList<String>();        for (int i = 0; i < c.length; i++) {            list.add(c[i]);        }        child.add(list);    }    class MyAdapter extends BaseExpandableListAdapter {        @Override        public int getGroupCount() {            // TODO Auto-generated method stub            return group.size();        }        @Override        public int getChildrenCount(int groupPosition) {            // TODO Auto-generated method stub            return child.size();        }        @Override        public Object getGroup(int groupPosition) {            // TODO Auto-generated method stub            return group.get(groupPosition);        }        @Override        public Object getChild(int groupPosition, int childPosition) {            // TODO Auto-generated method stub            return child.get(groupPosition).get(childPosition);        }        @Override        public long getGroupId(int groupPosition) {            // TODO Auto-generated method stub            return groupPosition;        }        @Override        public long getChildId(int groupPosition, int childPosition) {            // TODO Auto-generated method stub            return childPosition;        }        @Override        public boolean hasStableIds() {            // TODO Auto-generated method stub            return false;        }        @Override        public View getGroupView(int groupPosition, boolean isExpanded,                View convertView, ViewGroup parent) {            // TODO Auto-generated method stub            TextView textView = null;            if (convertView == null) {                textView = new TextView(MainActivity.this);            }            else{                textView=(TextView)convertView;            }            textView.setText(group.get(groupPosition));            textView.setTextSize(30);            textView.setPadding(32,10, 10, 20);            return textView;        }        @Override        public View getChildView(int groupPosition, int childPosition,                boolean isLastChild, View convertView, ViewGroup parent) {            // TODO Auto-generated method stub            TextView textView = null;            if (convertView == null) {                textView = new TextView(MainActivity.this);            }            else{                textView=(TextView)convertView;            }            textView.setText(child.get(groupPosition).get(childPosition));            textView.setTextSize(20);            textView.setPadding(60, 10, 0, 10);            return textView;        }        @Override        public boolean isChildSelectable(int groupPosition, int childPosition) {            // TODO Auto-generated method stub            return true;        }    }}