Android: ListView, problem with rounded corners

ⅰ亾dé卋堺 提交于 2019-12-19 03:58:23

问题


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?


回答1:


public class ClippedListView extends ListView {

/**
 * @param context
 */
public ClippedListView(Context context) {
    super(context);
}

/**
 * @param context
 * @param attrs
 */
public ClippedListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void dispatchDraw(Canvas canvas) {
    float radius = 10.0f;
    Path clipPath = new Path();
    RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
    clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
    canvas.clipPath(clipPath);
    super.dispatchDraw(canvas);
}
}

First clipping was not working, then using

setLayerType(View.LAYER_TYPE_SOFTWARE, null)

on my clippedListView solve the issue. My items' background are not messing with my corners anymore!




回答2:


Well, if you need this background for the items in the list, you should attach the background to the item layout, and not to the list view.



来源:https://stackoverflow.com/questions/6647121/android-listview-problem-with-rounded-corners

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!