参考:http://blog.youkuaiyun.com/jj120522/article/details/7944484听说还可以用9path图片的方式,但下面是用shape的方式。
首先,要准备shape,如下:
这个是ListView的背景shape:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- ListView的完整背景的shape.圆角背景 -->
<stroke
android:width="1dp"
android:color="@color/gray"/>
<solid
android:color="@color/white"/>
<corners android:radius="8dp"/>
</shape>
然后,为最前面被选择item的shape:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- listview的第一项selector后背景的shape -->
<stroke
android:width="1dp"
android:color="@color/gray"/>
<solid android:color="@color/gray"/>
<corners
android:topLeftRadius="8dp"
android:topRightRadius="8dp"/>
</shape>
最后面的item:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- ListView底部selector后背景 -->
<stroke
android:width="1dp"
android:color="@color/gray"/>
<solid android:color="@color/gray"/>
<corners
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"/>
</shape>
中间的item:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- ListView中间selector后背景 -->
<stroke
android:width="1dp"
android:color="@color/gray"/>
<solid android:color="@color/gray"/>
</shape>
shape设置完毕之后,接下来自定义ListView,继承ListView,重写onInterceptTouchEvent方法:
package com.nassoft.infomed.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.nassoft.infomed.R;
/**
*
* @ClassName: UC_RoundedListView
* @Description: TODO(圆角自定义ListView)
* @author zyl
* @date 2012-9-12 下午3:30:43
*/
public class UC_RoundedListView extends ListView {
public UC_RoundedListView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public UC_RoundedListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public UC_RoundedListView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
/**
* 拦截触摸事件
*/
@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.listview_onlyone_shape);
} else {
// 第一项
setSelector(R.drawable.listview_top_shape);
}
} else if (itemnum == (getAdapter().getCount() - 1))
// 最后一项
setSelector(R.drawable.listview_bottom_shape);
else {
// 中间项
setSelector(R.drawable.listview_center_shape);
}
}
break;
case MotionEvent.ACTION_UP:
break;
}
return super.onInterceptTouchEvent(ev);
}
/***
* 动态设置listview的高度
*
* @param listView
*/
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
// params.height += 5;// if without this statement,the listview will be
// a
// little short
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
listView.setLayoutParams(params);
}
}