[Android 开源项目学习]Android的UITableView(1)
最近由于项目加急,手里有好多看了差不多的开源项目,其中好多是大家经常用到的。图片的缓存BitmapFun(Android的文档中),AfinalMap,下拉刷新PullToRefresh等等吧,不过由于项目需要,我要就简要说下今天我要讲的这个组件,这个组件的项目地址.
看着左边的猪脚是不是觉得这个很像IOS中的UITableView,不过我告诉这是Android的ListView。
没看代码前,我看到作者说这个是a customized ListView,这是不敢相信,ListView是每一项都基本上一样,看看我们猪脚,怎么看都不像,不过后来一看代码,这不用ListView还用什么呢。
在开始前,先扯远点,在Android中感觉组件中最常用且最有个性且最复杂的就是ListView了,你想想PullToRefreshListView,SwipToDismiss 以及后来的点击展开的那个叫ExpandableList,无一不是ListView的杰出代表。貌似GoogleMail客户端的ListView挺有意思,有兴趣看看他是怎么实现的。
对了,我的3DListView翻译也是ListView开始的,尽管没写完剩下的文章,不过我会补上的。
为什么要用到UITableView
因为要做一个定位选择机构的页面,和美团、糯米的客户端定位很相似,上面是个定位的Item,下面显示的各位城市的Item,这个在IOS那肯定是用UITableView,且我的IOS同时也在做这个页面,的确就是用UITableView。
一个分组下,是几个Item。如热门城市:北京 武汉 信阳 全部城市:A 鞍山市 安庆市……信阳市……资阳市
实现原理
看看这个UITableView样式,其实就两种Item,每个分组(Group)的顶部标题(Header)和每个分组的列表选项(Cell)。
为了使ListVIew的positiom和UITableView中对应的分组(Group)和分组中的位置(Row),我们需要一个对应二者的一个数据结构,我们把它定为IndexPath。
我们需要根据数据分组,每个分组里都要一个Header和多个Cell,而在Listview中无论Cell亦或是Header我们统一认为它是一个Item,只是这个Item的IndexPath的属性不一样,他可能是一个Header,那个对应他的View就是一个HeaderView,同理Cell对应的是CellView,这个是不是可以理解呢。
用法
先去Github上现在UITableViewUI库:下载地址
建立一个自己的项目。
public class MainActivity extends Activity implements OnClickListener {
UITableView tableView;
private Button btnChange = null;
SimpleUITableViewAdapter tableViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tableViewAdapter = new SimpleUITableViewAdapter();
tableView = (UITableView) findViewById(R.id.listView);
tableView.setAdapter(tableViewAdapter);
tableView.setOnCellClickListener(tableViewAdapter);
tableView.setOnCellLongClickListener(tableViewAdapter);
tableView.setOnCellAccessoryClickListener(tableViewAdapter);
tableView.setOnHeaderClickListener(tableViewAdapter);
tableView.setOnHeaderLongClickListener(tableViewAdapter); btnChange = (Button) findViewById(R.id.btnChange);
btnChange.setOnClickListener(this); }
@Override
public void onClick(View v) {
int id = v.getId();
if(id == R.id.btnChange){
tableViewAdapter.setChange();
} } class SimpleUITableViewAdapter extends UITableViewAdapter implements OnCellClickListener, OnCellLongClickListener, OnCellAccessoryClickListener, OnHeaderClickListener, OnHeaderLongClickListener { private int[] color_line1_default;
private int[] color_line2_default;
private int[] color_line1_pressed;
private int[] color_line2_pressed; public SimpleUITableViewAdapter() {
// Prepare two sets of colors for odd and even lines
color_line1_default = new int[] { getResources().getColor(R.color.base_start_color_line1_default), getResources().getColor(R.color.base_start_color_line1_default) };
color_line2_default = new int[] { getResources().getColor(R.color.base_start_color_line2_default), getResources().getColor(R.color.base_start_color_line2_default) };
color_line1_pressed = new int[] { getResources().getColor(R.color.base_start_color_line1_pressed), getResources().getColor(R.color.base_start_color_line1_pressed) };
color_line2_pressed = new int[] { getResources().getColor(R.color.base_start_color_line2_pressed), getResources().getColor(R.color.base_start_color_line2_pressed) };
}
int number = 4;
int rad = 1;
public void setChange(){
number = 3;
rad = 2;
} @Override
public int numberOfGroups() {
return number;
} @Override
public int numberOfRows(int group) {
return (group + rad) * 2;
} @Override
public UITableHeaderItem headerItemForGroup(Context context, IndexPath indexPath) {
return new UITableHeaderItem("Group " + indexPath.getGroup());
} @Override
public UITableCellItem cellItemForRow(Context context, IndexPath indexPath) {
String title = "Cell number " + indexPath.getRow() + " in group " + indexPath.getGroup();
String subtitle = (indexPath.getRow() % 2 == 0) ? "Subtitle " + indexPath.getRow() : null;
return new UITableCellItem(title, subtitle);
} @Override
public UITableHeaderView headerViewForGroup(Context context, IndexPath indexPath, UITableHeaderItem headerItem, UITableHeaderView convertView) {
UITableHeaderView headerView;
if (convertView == null) {
// If the recycled view is null, we just creating one
headerView = new UITableHeaderView(context, indexPath);
} else {
headerView = (UITableHeaderView) convertView;
} headerView.setTitle(headerItem.title); return headerView;
}
@Override
public UITableCellView cellViewForRow(Context context, IndexPath indexPath, UITableCellItem cellItem, UITableCellView convertView) {
UITableCellView cellView;
if (convertView == null) {
// If the recycled view is null, we just creating one with cell's commons parameters
cellView = new UITableCellView(context, indexPath);
cellView.setMinimumHeight(80);
cellView.setAccessory(AccessoryType.DISCLOSURE);
} else {
cellView = (UITableCellView) convertView;
} cellView.setTitle(cellItem.title);
cellView.setSubtitle(cellItem.subtitle); // Set alternated background color
if (indexPath.getRow() % 2 == 0) {
cellView.setBackgroundColor(color_line1_default, color_line1_pressed);
} else {
cellView.setBackgroundColor(color_line2_default, color_line2_pressed);
} return cellView;
} @Override
public void onCellClick(IndexPath indexPath) {
Toast.makeText(getApplicationContext(), "Cell clicked : " + indexPath, 1000).show();
} @Override
public boolean onCellLongClick(IndexPath indexPath) {
Toast.makeText(getApplication(), "Cell long clicked : " + indexPath, 1000).show();
return indexPath.getRow() % 2 == 0; // Consume the long click one row out of two
} @Override
public void onCellAccessoryClick(IndexPath indexPath) {
Toast.makeText(getApplication(), "Cell accessory clicked : " + indexPath, 1000).show();
} @Override
public void onHeaderClick(IndexPath indexPath) {
Toast.makeText(getApplicationContext(), "Header clicked : " + indexPath, 1000).show();
} @Override
public boolean onHeaderLongClick(IndexPath indexPath) {
Toast.makeText(getApplicationContext(), "Header long clicked : " + indexPath, 1000).show();
return indexPath.getGroup() % 2 == 0; // Consume the long click one row out of two
}
} }
看下这个SimpleUITableViewAdapter 的实现,headerItemForGroup ()和cellItemForRow ()这个是需要的item数据,headerViewForGroup()和cellViewForRow是为分组的顶部标题和分组中的列表选项的试图展示。numberOfGroups()numberOfRows(int group)则表示每个分组的个数和对应group分组下的选项个数。
这个例子看着你不是觉得这个很有意思啊。
下一篇我们就可以看一下他的项目设计详细设计吧。
[Android 开源项目学习]Android的UITableView(1)的更多相关文章
- android开源项目学习
FBReaderJ FBReaderJ用于Android平台的电子书阅读器,它支持多种电子书籍格式包括:oeb.ePub和fb2.此外还支持直接读取zip.tar和gzip等压缩文档. 项目地址:ht ...
- android 开源项目学习
1.Android团队提供的示例项目 如果不是从学习Android SDK中提供的那些样例代码开始,可能没有更好的方法来掌握在Android这个框架上开发.由Android的核心开发团队提供了15个优 ...
- android 开源项目学习<二>
roottools: RootTools gives Rooted developers easy access to common rooted tools... https://code.g ...
- Android 开源项目及其学习
Android 系统研究:http://blog.csdn.net/luoshengyang/article/details/8923485 Android 腾讯技术人员博客 http://hukai ...
- Android 开源项目维护者宣布退出
Android开源项目(Android Open Source Project,AOSP)的长期维护者Jean-Baptiste Quéru在Google+上宣布退出,他退出AOSP项目的原因被认为与 ...
- Android开源项目分包方式学习(eoe、oschina、github)
总感觉Android中关于分包的文章很少,或者几乎可以说没有.但是合理地分包,又可以使整个项目模块化,减少包与包之间的依赖,让整个项目的框架更加清晰,更利于后续功能的拓展. 因为没有相关的文章,所以这 ...
- Android开源项目SlidingMenu本学习笔记(两)
我们已经出台SlidingMenu使用:Android开源项目SlidingMenu本学习笔记(一个),接下来再深入学习下.依据滑出项的Menu切换到相应的页面 文件夹结构: watermark/2/ ...
- GitHub 优秀的 Android 开源项目(转)
今天查找资源时看到的一篇文章,总结了很多实用资源,十分感谢原作者分享. 转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介 ...
- GitHub 优秀的 Android 开源项目
转自:http://blog.csdn.net/shulianghan/article/details/18046021 主要介绍那些不错个性化的View,包括ListView.ActionBar.M ...
随机推荐
- HDU 3920Clear All of Them I(状压DP)
HDU 3920 Clear All of Them I 题目是说有2n个敌人,现在可以发n枚炮弹,每枚炮弹可以(可以且仅可以)打两个敌人,每一枚炮弹的花费等于它所行进的距离,现在要消灭所有的敌人 ...
- 在VSTO界面中,调用xll中的函数
最近研究各种有点迷茫了,原来Xll的加载宏直接可以在C#中调用的,我又各种Out了. 先说明一下,在VBA中,如何调用吧 XLLFound = Application.RegisterXLL(This ...
- CString转换成int CString类相应函数
CString 型转化成 int 型 把 CString 类型的数据转化成整数类型最简单的方法就是使用标准的字符串到整数转换例程. 虽然通常你怀疑使用_atoi()函数是一个好的选择,它也很少会是一个 ...
- Map排序——按key排序,按value排序
注:转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5959279.html 上一篇博文谈到了集合类的自定义排序方式,那么进一步扩展开来,与集合同等重要的Map有 ...
- 转载:CSS3 圆角(border-radius)
前缀 例1 例2:无边框 书写顺序 其它 支持性 值:半径的长度 前缀 -moz(例如 -moz-border-radius)用于Firefox -webkit(例如:-webkit-border-r ...
- SQLServer: 无法修改表
工具—选项—Designers—表设计器和数据库设计器—阻止保存要求重新创建表的更改前的勾去掉.
- <创建和销毁对象>经验法则——考虑用静态工厂方法代替公有构造方法
一.引出静态工厂方法 对于java类而言,为了让使用者获取它自身的一个实例化对象,会有以下方法: 1.该类提供一个公有的构造方法.在这种情况下,程序可以通过多个“new 构造方法”语句来创建类的任意多 ...
- Parallel.ForEach , ThreadPool.QueueUserWorkItem
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- [转]AsyncDisplayKit 教程:达到 60 FPS 的滚动帧率
[原文:https://github.com/nixzhu/dev-blog/blob/master/2014-11-22-asyncdisplaykit-tutorial-achieving-60- ...
- 【M17】考虑使用缓式评估
1.缓式评估其实就是拖延战术,直到逼不得已的时候才去计算.缓式评估的使用场景有: 2.引用计数,考虑String,String是一个内含char指针(char指针以'\0'结束)的资源管理类,正常情况 ...
