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

ExpandableListActivity和SimpleExpandableListAdapter

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<? extends Map<String, ?>> groupData, int groupLayout, String[] groupFrom, int[] groupTo, List<? extends List<? extends Map<String, ?>>> 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”);
group.add(item1);
group.add(item2);

//一级条目第一项所需要的二级条目
List<Map<String, String>> subgroup1 = new ArrayList<Map<String, String>>();
Map<String, String> subgroup1Data1 = new HashMap<String, String>();
subgroup1Data1.put(“subgroup”, “groupData1”);
subgroup1.add(subgroup1Data1);
Map<String,String> subgroup1Data2 = new HashMap<String,String>();
subgroup1Data2.put(“subgroup”, “groupData2”);
subgroup1.add(subgroup1Data2);

//一级条目第二项所需要的二级条目
List<Map<String, String>> subgroup2 = new ArrayList<Map<String, String>>();
Map<String, String> subgroup2Data1 = new HashMap<String, String>();
subgroup2Data1.put(“subgroup”, “subGroupData1”);
subgroup2.add(subgroup2Data1);
Map<String, String> subgroup2Data2 = new HashMap<String, String>();
subgroup2Data2.put(“subgroup”, “subGroupData2”);
subgroup2.add(subgroup2Data2);

//保存所有二级条目的数据
List<List<Map<String, String>>> subGroup = new ArrayList<List<Map<String, String>>>();
subGroup.add(subgroup1);
subGroup.add(subgroup2);

/**
* 使用SimpleExpandableListAdapter创建一个对象
* Context context
* List<? extends Map<String, ?>> groupData 一级条目的数据
* int expandedGroupLayout 一级条目的布局文件
* String[] groupFrom 一级条目的key
* int[] groupTo 一级条目控件的ID
* List<? extends List<? extends Map<String, ?>>> 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 });
setListAdapter(sela);

RatingBar

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{

@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
        boolean fromUser) {
    System.out.println(rating);
}

}

SeekBar

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.setMax(100);
seekBar.setOnSeekBarChangeListener(new SeekBarListener());

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

private class SeekBarListener implements SeekBar.OnSeekBarChangeListener{
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
System.out.println(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
System.out.println(seekBar.getProgress());
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
System.out.println(seekBar.getProgress());
}
}

(特别说明:本文部分内容是在观看marschen的Android视频教程时做的笔记,感谢marschen推出的视频教程,这里也推荐给大家:http://www.marschen.com/portal.php)

arthinking wechat
欢迎关注itzhai公众号