开发使用到的数据统计时可以用分组的ExpandablelistView
效果:
第一步.layout中定义ExpandablelistView,和定义普通的listview一样
<RelativeLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"xmlns:tools&#61;"http://schemas.android.com/tools"android:layout_width&#61;"match_parent"android:layout_height&#61;"match_parent"android:paddingBottom&#61;"&#64;dimen/activity_vertical_margin"android:paddingLeft&#61;"&#64;dimen/activity_horizontal_margin"android:paddingRight&#61;"&#64;dimen/activity_horizontal_margin"android:paddingTop&#61;"&#64;dimen/activity_vertical_margin"tools:context&#61;"com.example.myexpandablelistview.MainActivity" ><ExpandableListViewandroid:id&#61;"&#64;&#43;id/mylistview"android:layout_width&#61;"match_parent"android:layout_height&#61;"match_parent" >ExpandableListView>RelativeLayout>
第二步&#xff0c;设置组和子的数据源
第三步&#xff0c;自定义myadapter继承BaseExpandableListAdapter&#xff0c;复写方法
第四部&#xff0c;给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&#61;{"三国人物","水浒人物","西游人物"};//子视图数据String[][] children&#61;{{"曹操","孙权","刘备"},{"宋江","吴用","高俅","林冲"},{"孙悟空","红孩儿","哪吒"}};&#64;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mylistview&#61;(ExpandableListView) findViewById(R.id.mylistview);mylistview.setAdapter(new Myadapter());}//自定义adapterclass Myadapter extends BaseExpandableListAdapter{&#64;Override//组数public int getGroupCount() {return parents.length;}&#64;Override//每组的子数public int getChildrenCount(int groupPosition) {return children[groupPosition].length;}&#64;Override//返回对应的组public Object getGroup(int groupPosition) {return parents[groupPosition];}&#64;Override//返回对应的子public Object getChild(int groupPosition, int childPosition) {return children[groupPosition][childPosition];}&#64;Override//返回组idpublic long getGroupId(int groupPosition) {return groupPosition;}&#64;Override//返回子idpublic long getChildId(int groupPosition, int childPosition) {return childPosition;}&#64;Overridepublic boolean hasStableIds() {return true;}&#64;Override//返回组试图public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {TextView view;if(convertView!&#61;null){view&#61;(TextView) convertView;}else{view &#61;new TextView(MainActivity.this);}view.setText(parents[groupPosition]);view.setBackgroundColor(Color.RED);return view;}&#64;Override//返回子试图public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {TextView view;if(convertView!&#61;null){view&#61;(TextView) convertView;}else{view &#61;new TextView(MainActivity.this);}view.setText(children[groupPosition][childPosition]);return view;}&#64;Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}}}