您的位置:首页 > 移动开发 > Android开发

Android中ListView列表Item的圆角效果实现

2014-01-21 20:12 555 查看
先看一张图,这是微信中的设置界面。





  列表Item第一项需要上圆角,最后一项下圆角,若只有一项,上下四个圆角。按下效果也一样。在中间则不需要圆角。

  解决方案同网上其他开发者答案差不多一样,在这里我自己只是做一个标记。

1. 先定义四种按下效果的drawable。

1.下方圆角

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#e5e5e5"
android:endColor="#e5e5e5"
android:angle="270"/>
<corners android:bottomLeftRadius="6dip"
android:bottomRightRadius="6dip" />
</shape>


2.上方圆角

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#e5e5e5"
android:endColor="#e5e5e5"
android:angle="270"/>
<corners android:topLeftRadius="6dip"
android:topRightRadius="6dip"/>
</shape>


3.上下都圆角

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#e5e5e5"
android:endColor="#e5e5e5"
android:angle="270"/>
<corners android:topLeftRadius="6dip"
android:topRightRadius="6dip"
android:bottomLeftRadius="6dip"
android:bottomRightRadius="6dip"/>
</shape>


4. 没有圆角

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#e5e5e5"
android:endColor="#e5e5e5"
android:angle="270"/>
</shape>


将这四个xml文件命名好放在drawable下。

再来重写我们自定义的ListView。这里主要重写的是ListView的onInterceptTouchEvent方法。

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


这个逻辑我就不多做介绍了,应该很容易看得懂。就是判断行号来设置不同的Selector。

学习过程中做一笔记。转载请注明出处~~~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: