原文:http://blog.csdn.net/ly_rose/article/details/50358433
还有一篇比较完整:http://blog.csdn.net/sysukehan/article/details/51960473
创建工程Android_expandableListView中添加控件ExpandableListView
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="com.example.android_expandablelistview.MainActivity" >
- <ExpandableListView
- android:id="@+id/expandableListView1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true" >
- </ExpandableListView>
- </RelativeLayout>
- package com.example.android_expandablelistview;
- import java.util.ArrayList;
- import java.util.List;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseExpandableListAdapter;
- import android.widget.ExpandableListView;
- import android.widget.TextView;
- public class MainActivity extends ActionBarActivity {
- private ExpandableListView listView;
- private MyAdapter adapter;
- private List<String> group;//组
- private List<List<String>> child;//子
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- listView=(ExpandableListView) this.findViewById(R.id.expandableListView1);
- adapter=new MyAdapter();
- initData();
- listView.setAdapter(adapter);
- listView.setGroupIndicator(null);//把箭头去掉
- }
- //初始化数据,组和子元素实例化
- public void initData(){
- group=new ArrayList<String>();
- child=new ArrayList<List<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(36, 10, 0, 10);
- 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(72, 10, 0, 10);
- return textView;
- }
- @Override
- public boolean isChildSelectable(int groupPosition, int childPosition) {
- // TODO Auto-generated method stub
- return true;
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
运行效果图如下
去掉箭头,加上
- listView.setGroupIndicator(null);//把箭头去掉