0%
这是一片思考的空间 -- arthinking
Spring 重构&代码整洁之道 软件设计 JVM 并发编程 数据结构与算法 分布式 存储 网络 微服务 设计模式
Java技术栈 - 涉及Java技术体系

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> 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");
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> 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 });
    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)

欢迎关注我的其它发布渠道

订阅IT宅
内功修炼
Java技术栈
Java架构杂谈是IT宅精品文章公众号,欢迎订阅:
📄 网络基础知识:两万字长文50+张趣图带你领悟网络编程的内功心法 📄 HTTP发展史:三万长文50+趣图带你领悟web编程的内功心法 📄 HTTP/1.1:可扩展,可靠性,请求应答,无状态,明文传输 📄 HTTP/1.1报文详解:Method,URI,URL,消息头,消息体,状态行 📄 HTTP常用请求头大揭秘 📄 HTTPS:网络安全攻坚战 📄 HTTP/2:网络安全传输的快车道 📄 HTTP/3:让传输效率再一次起飞 📄 高性能网络编程:图解Socket核心内幕以及五大IO模型 📄 高性能网络编程:三分钟短文快速了解信号驱动式IO 📄 高性能网络编程:彻底弄懂IO复用 - IO处理杀手锏,带您深入了解select,poll,epoll 📄 高性能网络编程:异步IO:新时代的IO处理利器 📄 高性能网络编程:网络编程范式 - 高性能服务器就这么回事 📄 高性能网络编程:性能追击 - 万字长文30+图揭秘8大主流服务器程序线程模型
📄 Java内存模型:如果有人给你撕逼Java内存模型,就把这些问题甩给他 📄 一文带你彻底理解同步和锁的本质(干货) 📄 AQS与并发包中锁的通用实现 📄 ReentrantLock介绍与使用 📄 ReentrantReadWriteLock介绍与使用 📄 ReentrantLock的Condition原理解析 📄 如何优雅的中断线程 📄 如何优雅的挂起线程 📄 图解几个好玩的并发辅助工具类 📄 图解BlockingQueue阻塞队列
📄 消息队列那么多,为什么建议深入了解下RabbitMQ? 📄 高并发异步解耦利器:RocketMQ究竟强在哪里? 📄 Kafka必知必会18问:30+图带您看透Kafka
📄 洞悉MySQL底层架构:游走在缓冲与磁盘之间 📄 SQL运行内幕:从执行原理看调优的本质 📄 洞悉Redis技术内幕:缓存,数据结构,并发,集群与算法