I have ListView with rounded corners implemented like bellow.
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/list_bg"
android:divider="#dbdbdb"
android:dividerHeight="1sp"
android:cacheColorHint="#00000000"
android:descendantFocusability="afterDescendants"
android:scrollbars="vertical">
</ListView>
where list_bg.xml is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff"/>
<corners android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
/>
</shape>
This give me rounded corners. The problem is that each items are RelativeLayout with TextView on the left side and ImageButton on the right side. Both views have custom graphic on pressed, foucused and default state. Something like this.
+------------------------------------+
| TextView | ImageButton |
+------------------------------------+
The problem is that custom graphic cover ListView background and thus rounded corners on first and last item. Everything is okay when scrolling.
I've written custom adapter and in getView method I set proper bg for items.
@Override
public View getView(int position, View view, ViewGroup parent) {
...
if(position == 0) {
mItemView.setBackgroundResource(R.drawable.cell_item_first_selector);
mImgBtn.setBackgroundResource(R.drawable.cell_btn_first_selector);
}
else if (position == mData.size() -1) {
mItemView.setBackgroundResource(R.drawable.cell_item_last_selector);
mImgBtn.setBackgroundResource(R.drawable.cell_btn_last_selector);
}
else {
mItemView.setBackgroundResource(R.drawable.cell_item_def_selector);
mImgBtn.setBackgroundResource(R.drawable.cell_btn_def_selector);
}
...
}
It works, but I'm not sure that is good solution. Is there any other way to automatically round corners in Android Views, not just by setting background?