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"
?>
<
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"
?>
<
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"
?>
<
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"
?>
<
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"
?>
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:
最后
只要是程序员,不管是Java还是Android,如果不去阅读源码,只看API文档,那就只是停留于皮毛,这对我们知识体系的建立和完备以及实战技术的提升都是不利的。
真正最能锻炼能力的便是直接去阅读源码,不仅限于阅读各大系统源码,还包括各种优秀的开源库。
腾讯、字节跳动、阿里、百度等BAT大厂 2019-2021面试真题解析
资料太多,全部展示会影响篇幅,暂时就先列举这些部分截图
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
资料太多,全部展示会影响篇幅,暂时就先列举这些部分截图
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!