Android中圆角列表ListView

2.原理

通过判断ListView上点击的项的位置,我们切换不同的选择器,当然这个切换的动作我们需要定义在重写ListView的onInterceptTouchEvent()方法中。

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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文件定义如下:

?

1
2
3
4
5
6
7
8
9
10
<? 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定义如下:

?

1
2
3
4
5
6
7
8
<? 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定义如下:

?

1
2
3
4
5
6
7
8
<? 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定义如下:

?

1
2
3
4
5
6
<? 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 >

4.背景图片

因为默认的情况下,ListView就要显示一个圆角的边框,这个我们使用一张9patch背景图片来实现app_list_corner_border.9.png:

在这里提示一下,做9patch背景图片的时候,记得把内容区域定义为边框线以内的区域。

5. 初步实现

参考前面提供的素材和核心代码,我们初步实现如下:

(1).自定义CornerListView.java:

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* 圆角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:

?

1
2
3
4
5
6
7
8
9
10
11
12
13
<? 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:

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

结尾

最后小编想说:不论以后选择什么方向发展,目前重要的是把Android方面的技术学好,毕竟其实对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!

当程序员容易,当一个优秀的程序员是需要不断学习的,从初级程序员到高级程序员,从初级架构师到资深架构师,或者走向管理,从技术经理到技术总监,每个阶段都需要掌握不同的能力。早早确定自己的职业方向,才能在工作和能力提升中甩开同龄人。

想要拿高薪实现技术提升薪水得到质的飞跃。最快捷的方式,就是有人可以带着你一起分析,这样学习起来最为高效,所以为了大家能够顺利进阶中高级、架构师,我特地为大家准备了一套高手学习的源码和框架视频等精品Android架构师教程,保证你学了以后保证薪资上升一个台阶。

当你有了学习线路,学习哪些内容,也知道以后的路怎么走了,理论看多了总要实践的。

高级UI,自定义View

UI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。

不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

…(img-eQ589tuy-1713786228525)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值