开发使用到的数据统计时可以用分组的ExpandablelistView

效果:

第一步.layout中定义ExpandablelistView,和定义普通的listview一样

<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.myexpandablelistview.MainActivity" > <ExpandableListView
android:id="@+id/mylistview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ExpandableListView> </RelativeLayout>

第二步,设置组和子的数据源

第三步,自定义myadapter继承BaseExpandableListAdapter,复写方法

第四部,给listview设置adapter

package com.example.myexpandablelistview;

import android.app.Activity;
import android.graphics.Color;
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 Activity {
private ExpandableListView mylistview;
//组视图数据
String[] parents={"三国人物","水浒人物","西游人物"};
//子视图数据
String[][] children={
{"曹操","孙权","刘备"},{"宋江","吴用","高俅","林冲"},{"孙悟空","红孩儿","哪吒"}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mylistview=(ExpandableListView) findViewById(R.id.mylistview);
mylistview.setAdapter(new Myadapter());
}
//自定义adapter
class Myadapter extends BaseExpandableListAdapter{ @Override
//组数
public int getGroupCount() { return parents.length;
} @Override
//每组的子数
public int getChildrenCount(int groupPosition) { return children[groupPosition].length;
} @Override
//返回对应的组
public Object getGroup(int groupPosition) { return parents[groupPosition];
} @Override
//返回对应的子
public Object getChild(int groupPosition, int childPosition) { return children[groupPosition][childPosition];
} @Override
//返回组id
public long getGroupId(int groupPosition) { return groupPosition;
} @Override
//返回子id
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) {
TextView view;
if(convertView!=null){
view=(TextView) convertView;
}
else{
view =new TextView(MainActivity.this);
}
view.setText(parents[groupPosition]);
view.setBackgroundColor(Color.RED);
return view;
} @Override
//返回子试图
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) { TextView view;
if(convertView!=null){
view=(TextView) convertView;
}
else{
view =new TextView(MainActivity.this);
}
view.setText(children[groupPosition][childPosition]);
return view;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition) { return true;
} } }
05-11 16:12