Android学习系列(16)--App列表之圆角ListView
直角看多了,就想看看圆角,不知何时,这几年刮起了一阵阵的圆角设计风:CSS新标准纳入圆角元素,iphone中几乎随处可见圆角设计,也开始出现很多圆角名片了...
今天我们就实现一个圆角的ListView效果。
圆角的设计,我们并不追求到处都用,无处不用,android中有少数界面用直角确实容易显得锋利,和周边界面太过对比而显得不协调,比如大栏目列表,设置等等,而采用圆角实现,则会活泼,轻松的多,也融合的特别好。
实际上在Android中因为SDK中没有默认对圆角的一个完整的支持,需要麻烦自定义设置才能实现完美的圆角效果,所以绝大多数应用都是采用分组直角列表这种样式。
所以我觉得很有必要让大家看看这些少数的不一样的东西,看看有什么不一样的感觉。
先让我们来看两张图片:
<ignore_js_op> <ignore_js_op>
ps:上述只是效果,并不是说左边的圆角列表就是用listview是实现的,事实上它是用LinearLayout布局一个一个堆起来的。
通过判断ListView上点击的项的位置,我们切换不同的选择器,当然这个切换的动作我们需要定义在重写ListView的onInterceptTouchEvent()方法中。
- if(itemnum==0){
- if(itemnum==(getAdapter().getCount()-1)){
- //只有一项
- setSelector(R.drawable.app_list_corner_round);
- }else{
- //第一项
- setSelector(R.drawable.app_list_corner_round_top);
- }
- }else if(itemnum==(getAdapter().getCount()-1))
- //最后一项
- setSelector(R.drawable.app_list_corner_round_bottom);
- else{
- //中间一项
- setSelector(R.drawable.app_list_corner_shape);
- }
复制代码
3.定义选择器
如果只有一项,我们需要四个角都是圆角,app_list_corner_round.xml文件定义如下:
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android">
- <gradient android:startColor="#B5E7B8"
- android:endColor="#76D37B"
- android:angle="270"/>
- <corners android:topLeftRadius="4dip"
- android:topRightRadius="4dip"
- android:bottomLeftRadius="4dip"
- android:bottomRightRadius="4dip"/>
- </shape>
复制代码
如果是顶部第一项,则上面两个角为圆角,app_list_corner_round_top.xml定义如下:
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android">
- <gradient android:startColor="#B5E7B8"
- android:endColor="#76D37B"
- android:angle="270"/>
- <corners android:topLeftRadius="4dip"
- android:topRightRadius="4dip"/>
- </shape>
复制代码
如果是底部最后一项,则下面两个角为圆角,app_list_corner_round_bottom.xml定义如下:
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android">
- <gradient android:startColor="#B5E7B8"
- android:endColor="#76D37B"
- android:angle="270"/>
- <corners android:bottomLeftRadius="4dip"
- android:bottomRightRadius="4dip" />
- </shape>
复制代码
如果是中间项,则应该不需要圆角, app_list_corner_shape.xml定义如下:
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android">
- <gradient android:startColor="#B5E7B8"
- android:endColor="#76D37B"
- android:angle="270"/>
- </shape>
复制代码
因为默认的情况下,ListView就要显示一个圆角的边框,这个我们使用一张9patch背景图片来实现app_list_corner_border.9.png:

在这里提示一下,做9patch背景图片的时候,记得把内容区域定义为边框线以内的区域。
参考前面提供的素材和核心代码,我们初步实现如下:
(1).自定义CornerListView.java:
- /**
- * 圆角ListView
- */
- public class CornerListView extends ListView {
- public CornerListView(Context context) {
- super(context);
- }
- public CornerListView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
- public CornerListView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- @Override
- public boolean onInterceptTouchEvent(MotionEvent ev) {
- switch (ev.getAction()) {
- case MotionEvent.ACTION_DOWN:
- int x = (int) ev.getX();
- int y = (int) ev.getY();
- int itemnum = pointToPosition(x, y);
- if (itemnum == AdapterView.INVALID_POSITION)
- break;
- else
- {
- if(itemnum==0){
- if(itemnum==(getAdapter().getCount()-1)){
- setSelector(R.drawable.app_list_corner_round);
- }else{
- setSelector(R.drawable.app_list_corner_round_top);
- }
- }else if(itemnum==(getAdapter().getCount()-1))
- setSelector(R.drawable.app_list_corner_round_bottom);
- else{
- setSelector(R.drawable.app_list_corner_shape);
- }
- }
- break;
- case MotionEvent.ACTION_UP:
- break;
- }
- return super.onInterceptTouchEvent(ev);
- }
- }
复制代码
这个CornerListView主要处理了点击项的选择器的切换。
(2).列表布局文件和列表项布局文件:
列表布局文件main_tab_setting.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <com.tianxia.app.floworld.view.CornerListView android:id="@+id/setting_list"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dip"
- android:background="@drawable/app_list_corner_border"
- android:cacheColorHint="#00000000">
- </com.tianxia.app.floworld.view.CornerListView>
- </LinearLayout>
复制代码
列表项布局文件main_tab_setting_list_item.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ImageView android:id="@+id/setting_list_item_arrow"
- android:layout_alignParentRight="true"
- android:layout_centerVertical="true"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_marginLeft="15dip"
- android:layout_marginRight="15dip"
- android:src="@drawable/appreciate_tab_list_item_arrow_small"/>
- <TextView android:id="@+id/setting_list_item_text"
- android:layout_toLeftOf="@id/setting_list_item_arrow"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="16dip"
- android:textColor="#000000"
- android:paddingTop="10dip"
- android:paddingBottom="10dip"
- android:paddingLeft="10dip" />
- </RelativeLayout>
复制代码
(3)界面实现
显示界面SettingTabActivity.java:
- public class SettingTabActivity extends Activity{
- private CornerListView cornerListView = null;
- private List<Map<String,String>> listData = null;
- private SimpleAdapter adapter = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main_tab_setting);
- cornerListView = (CornerListView)findViewById(R.id.setting_list);
- setListData();
- adapter = new SimpleAdapter(getApplicationContext(), listData, R.layout.main_tab_setting_list_item , new String[]{"text"}, new int[]{R.id.setting_list_item_text});
- cornerListView.setAdapter(adapter);
- }
- /**
- * 设置列表数据
- */
- private void setListData(){
- listData = new ArrayList<Map<String,String>>();
- Map<String,String> map = new HashMap<String, String>();
- map.put("text", "图库更新");
- listData.add(map);
- map = new HashMap<String, String>();
- map.put("text", "收藏图片");
- listData.add(map);
- map = new HashMap<String, String>();
- map.put("text", "下载目录");
- listData.add(map);
- }
- }
复制代码
通过以上实现,我们基本达到了圆角的ListView的效果:
<ignore_js_op> <ignore_js_op>

Android学习系列(16)--App列表之圆角ListView的更多相关文章
- Android学习系列(17)--App列表之圆角ListView(续)
http://www.cnblogs.com/qianxudetianxia/archive/2011/09/19/2068760.html 本来这篇文章想并到上篇Android学习系列(16)- ...
- Android学习系列(15)--App列表之游标ListView(索引ListView)
游标ListView,提供索引标签,使用户能够快速定位列表项. 也可以叫索引ListView,有的人称也为Tweaked ListView,可能更形象些吧. 一看图啥都懂了: 1. ...
- Android学习系列(9)--App列表之分组ListView
吸引用户的眼球,是我们至死不渝的追求: 第一时间呈现最有价值的信息,简明大方,告诉客户,你的选择是多么的明智,这正是你寻觅已久的东西. 分组的应用场合还是很多的,有数据集合的地方 ...
- Android学习系列(11)--App列表之拖拽ListView(下)
接着上篇Android学习系列(10)--App列表之拖拽ListView(上)我们继续实现ListView的拖拽效果. 7.重写onTouchEvent()方法. 在这个方法中我们主要是处理 ...
- Android学习系列(10)--App列表之拖拽ListView(上)
研究了很久的拖拽ListView的实现,受益良多,特此与尔共飨. 鉴于这部分内容网上的资料少而简陋,而具体的实现过程或许对大家才有帮助,为了详尽而不失真,我们一步一步分析,分成两篇文章. ...
- Android学习系列(12)--App列表之拖拽GridView
根据前面文章中ListView拖拽的实现原理,我们也是很容易实现推拽GridView的,下面我就以相同步骤实现基本的GridView拖拽效果. 因为GridView不用做分组处理,代码处理起来 ...
- Android学习系列(7)--App轮询服务器消息
这篇文章是android开发人员的必备知识. 1.轮询服务器 一般的应用,定时通知消息可以采用轮询的方法从服务器拿取消息,当然实时消息通知的话,建议采用推送服务. 其中需要注意轮询的频率 ...
- Android学习系列(23)--App主界面实现
在上篇文章<Android学习系列(22)--App主界面比较>中我们浅略的分析了几个主界面布局,选了一个最大众化的经典布局.今天我们就这个经典布局,用代码具体的实现它. 1.预览图先看下 ...
- Android学习系列(18)--App工程结构搭建
本文算是一篇漫谈,谈一谈关于Android开发中工程初始化的时候如何在初期我们就能搭建一个好的架构. 关于android架构,因为手机的限制,目前我觉得也确实没什么大谈特谈的,但是从开发的 ...
随机推荐
- 【BZOJ】【4066】简单题(强制在线)
KD-Tree KD-Tree的进阶姿势戳这里 http://zyfzyf.is-programmer.com/posts/92431.html 为啥有种线段树&平衡树的即视感……(树形结构的 ...
- 【BZOJ】【2434】【NOI2011】阿狸的打字机
AC自动机+DFS序+BIT 好题啊……orz PoPoQQQ 大爷 一道相似的题目:[BZOJ][3172][TJOI2013]单词 那道题也是在fail树上数有多少个点,只不过这题是在x的fail ...
- PHP语言基础之MySql 05 By ACReaper
PHP的基本语法学完后,我们马上学下PHP如何和MySql进行交互.PHP和MySql进行交互的API可以分为两类,一类是面向过程的,一类是面向对象的,面向对象的我们等复习完面向对象再介绍,现在先介绍 ...
- DELPHI中千万别直接使用CreateThread ,建议使用BeginThread(在C++中无大问题,可是到了DELPHI中情况就不一样了)
以前在写个别程序的时候老是喜欢使用纯API编程. 在C++中无大问题,可是到了DELPHI中情况就不一样了. 当你用 DELPHI写的多线程程序莫名其妙的内存错误,特别是字符串(string)操作; ...
- Informatica 常用组件Lookup之二 已连接和未连接的查找
可以配置一个已连接的查找转换,以从映射管道中直接接收输入:您也可以配置一个未连接的查找转换,以从其它转换的表达式结果中接收输入. 已连接的查找 未连接的查找 直接从管道接收输入值. 从其它转换的 :L ...
- 好久没做codeforces
近期小结: 做了四场多校的比赛,感觉学到的东西好少诶,除了CLJ那场太神,其他场次的赛后几乎都能独立的AK 感觉顶多就锻炼锻炼代码能力?真是件伤感的事情... 虽然每场都,b,但只要baolaoban ...
- BigDecimal 小数 浮点数 精度 财务计算
简介 float和double类型的使用局限: 单精度浮点型变量float可以处理6~7位有效数,双精度浮点型变量double可以处理15~16位有效数,在实际应用中,如果需要对更大或者更小的数进行运 ...
- docker 基本原理及快速入门
作者地址:青牛 什么是docker Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目.它基于 Google 公司推出的 Go 语言实现. 项目后来 ...
- [HTML5] Avoiding CSS Conflicts via Shadow DOM CSS encapsulation
Shadow DOM is part of the web components specification. It allows us to ship self contained componen ...
- Android studio 2.0--android增量更新的那些事
用了这么久的AS 2.0预览版本号.4.7日谷歌最终公布了android studio 2.0正式版,小编当日便下载了.玩了一下.感觉第二次build编译明显快了,并且好像并没有又一次部署apk.经过 ...