Android中的常用控件之ExpandableListActivity SimpleExpandableListAdapter RatingBar SeekBar


public class SimpleExpandableListAdapter extends BaseExpandableListAdapter

An easy adapter to map static data to group and child views defined in an XML file. You can separately specify the data backing the group as a List of Maps. Each entry in the ArrayList corresponds to one group in the expandable list. The Maps contain the data for each row. You also specify an XML file that defines the views used to display a group, and a mapping from keys in the Map to specific views. This process is similar for a child, except it is one-level deeper so the data backing is specified as a List, where the first List corresponds to the group of the child, the second List corresponds to the position of the child within the group, and finally the Map holds the data for that particular child.

SimpleExpandableListAdapter(Context context, List> groupData, int groupLayout, String[] groupFrom, int[] groupTo, List>> childData, int childLayout, String[] childFrom, int[] childTo)

编写ExpandableListActivity的步骤: ① 在Activity的布局文件中添加一个ExpandableListView组件,组件ID使用系统自带ID:

② 另外新建一个布局文件group.xml文件用于设置一级条目的样式:

③ 新建一个布局文件child.xml文件用于设置二级条目的样式:

④ 新建一个类ListActivity继承ExpandableListActivity,在里面创建组件:

List<Map<String, String>> group = new ArrayList<Map<String, String>>();
Map<String, String> item1 = new HashMap<String, String>();
item1.put("group", "item1");
Map<String, String> item2 = new HashMap<String, String>();
item2.put("group", "item2");

List<Map<String, String>> subgroup1 = new ArrayList<Map<String, String>>();
Map<String, String> subgroup1Data1 = new HashMap<String, String>();
subgroup1Data1.put("subgroup", "groupData1");
Map<String,String> subgroup1Data2 = new HashMap<String,String>();
subgroup1Data2.put("subgroup", "groupData2");

List<Map<String, String>> subgroup2 = new ArrayList<Map<String, String>>();
Map<String, String> subgroup2Data1 = new HashMap<String, String>();
subgroup2Data1.put("subgroup", "subGroupData1");
Map<String, String> subgroup2Data2 = new HashMap<String, String>();
subgroup2Data2.put("subgroup", "subGroupData2");

List<List<Map<String, String>>> subGroup = new ArrayList<List<Map<String, String>>>();


  • 使用SimpleExpandableListAdapter创建一个对象
  • Context context
  • List> groupData 一级条目的数据
  • int expandedGroupLayout 一级条目的布局文件
  • String[] groupFrom 一级条目的key
  • int[] groupTo 一级条目控件的ID
  • List>> childData 二级条目的数据
  • int childLayout 二级条目的布局文件
  • String[] childFrom 二级条目的key
  • int[] childTo 二级条目的空间id
    SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
    this, group, R.layout.group, new String[] { "group" },
    new int[] { R.id.groupTo }, subGroup, R.layout.child,
    new String[] { "subgroup" }, new int[] { R.id.childTo });


public class RatingBar extends AbsSeekBar

A RatingBar is an extension of SeekBar and ProgressBar that shows a rating in stars. The user can touch/drag or use arrow keys to set the rating when using the default size RatingBar. The smaller RatingBar style ( ratingBarStyleSmall) and the larger indicator-only style (ratingBarStyleIndicator) do not support user interaction and should only be used as indicators. 编写RatingBar的基本步骤: ① 在Activity的布局文件中创建RatingBar组件:

② 在Activity中获取RatingBar并进行相应的操作:

RatingBar ratingBar = (RatingBar)findViewById(R.id.ratingbar01);
ratingBar.setOnRatingBarChangeListener(new RatingBarListener());

③ 为编写RatingBar的监听事件,实现RatingBar.OnRatingBarChangeListener,并并覆盖里面的onRatingChanged函数:

private class RatingBarListener implements RatingBar.OnRatingBarChangeListener{

public void onRatingChanged(RatingBar ratingBar, float rating,
		boolean fromUser) {



public class SeekBar extends AbsSeekBar

A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged. Clients of the SeekBar can attach a SeekBar.OnSeekBarChangeListener to be notified of the user's actions. 编写SeekBar的步骤: ① 在Activity的布局文件中添加SeekBar组件:

② 在Activity中获取SeekBar并进行控制:

SeekBar seekBar = (SeekBar)findViewById(R.id.seekbar01) ;
seekBar.setOnSeekBarChangeListener(new SeekBarListener());

③ 为编写SeekBar的监听器,实现SeekBar.OnSeekBarChangeListener,并覆盖里面的三个基本方法:

private class SeekBarListener implements SeekBar.OnSeekBarChangeListener{
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
public void onStartTrackingTouch(SeekBar seekBar) {
public void onStopTrackingTouch(SeekBar seekBar) {