Android中的常用控件之ExpandableListActivity SimpleExpandableListAdapter RatingBar SeekBar
本文由发表于6年前 | Android | 暂无评论 |  被围观 6,474 views+

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<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:

<ExpandableListView android:id="@id/android:list"
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent"
          android:drawSelectorOnTop="false"/>

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

<TextView android:id="@+id/groupTo" 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent" 
	android:paddingLeft="10px" 
	android:paddingTop="10px"
	android:paddingBottom="10px" 
	android:textSize="20sp" 
	android:text="No data" />

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

<TextView android:id="@+id/childTo" 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent" 
	android:paddingLeft="20px" 
	android:paddingTop="5px"
	android:paddingBottom="5px" 
	android:textSize="20sp" 
	android:text="No data" />

④ 新建一个类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组件:

<RatingBar
	android:id="@+id/ratingbar01"
	android:layout_height="wrap_content"
	android:layout_width="wrap_content"
	android:numStars="5"
	android:stepSize="1.0"
	/>

② 在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组件:

<SeekBar
	android:id="@+id/seekbar01"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	/>

② 在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)

除了文章中有特别说明,均为IT宅原创文章,转载请以链接形式注明出处。
本文链接:http://www.itzhai.com/android-are-commonly-used-controls-of-expandablelistactivity-simpleexpandablelistadapter-ratingbar-seekbar.html
arthinking Java技术交流群:280755654,入门群:428693174 more
分享到:
 
2011 7/13
文章评论
    没有评论
给我留言

有人回复时邮件通知我
Android的相关文章
随机文章 本月热门 热评
1 基于分享式的试题库系统 | Project 2011/11/14
2 JavaScript设计模式笔记汇总 | IT宅文章归档 AD 2012/12/28 2012/12/28
3 访问执行速度过慢分析之MySQL慢语句分析 2014/11/12
4 Android下载文件到SD卡 2011/7/12
5 【TODO】几个海量查找和去重的问题 2014/6/15
6 说说Scrum敏捷开发 2014/6/1
友情推荐 更多
破博客 文官洗碗安天下,武将打怪定乾坤。多么美好的年代,思之令人泪落。
Mr.5's Life 白天是一名程序员,晚上就是个有抱负的探索者
行知-追寻技术之美 关注大数据,分布式系统
我爱编程 编程成长轨迹
Cynthia's Blog 学习笔记 知识总结 思考感悟
 
猜您喜欢
欢迎关注我的公众号 IT宅
关于IT宅 文章归档

IT宅中的文章除了标题注明转载或有特别说明的文章,均为IT宅的技术知识总结,学习笔记或随笔。如果喜欢,请使用文章下面提供的分享组件。转载请注明出处并加入文章的原链接。 感谢大家的支持。

联系我们:admin@itzhai.com

Theme by arthinking. Copyright © 2011-2015 IT宅.com 保留所有权利.