安卓的ListView,问题圆角圆角、问题、ListView

2023-09-06 05:38:45 作者:怎么潇洒怎么活

我的ListView与像波纹管实现的圆角。

 < ListView控件的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID /列表
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:背景=@可绘制/ list_bg
    机器人:分隔=#dbdbdb
    机器人:dividerHeight =1SP
    机器人:cacheColorHint =#00000000
    机器人:descendantFocusability =afterDescendants
    机器人:滚动条=垂直>
< / ListView控件>
 

在这里list_bg.xml是:

 < XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:形状=矩形>
    [固体机器人:颜色=#FFFFFF/>
    <边角安卓bottomLeftRadius =10dp
            机器人:bottomRightRadius =10dp
            机器人:topLeftRadius =10dp
            机器人:topRightRadius =10dp
            />
< /形状>
 

这给我圆角。 的问题是,每个项是RelativeLayout的与TextView的左侧和的ImageButton右侧。这两种观点都对pressed,foucused和默认状态的自定义图形。事情是这样的。

  + ------------------------------------ +
| TextView中|的ImageButton |
+ ------------------------------------ +
 
Android ListView Item滑动删除的效果问题

的问题在于,自定义的图形覆盖的ListView背景和对第一和最后一个项目因而圆角。滚动时一切正常。

我已经写了自定义适配器和getView方法我设置适当的BG的项目。

  @覆盖
公共查看getView(INT的立场,观点来看,一个ViewGroup父){
    ...
    如果(位置== 0){
        mItemView.setBackgroundResource(R.drawable.cell_item_first_selector);
        mImgBtn.setBackgroundResource(R.drawable.cell_btn_first_selector);
    }
    否则,如果(位置== mData.size(-1)){
        mItemView.setBackgroundResource(R.drawable.cell_item_last_selector);
        mImgBtn.setBackgroundResource(R.drawable.cell_btn_last_selector);
    }
    其他 {
        mItemView.setBackgroundResource(R.drawable.cell_item_def_selector);
        mImgBtn.setBackgroundResource(R.drawable.cell_btn_def_selector);
    }
    ...
}
 

它的工作原理,但我不知道那是很好的解决方案。是否有任何其他的方式来自动圆角在Android的意见,而不是仅仅通过设置背景?

解决方案

 公共类ClippedListView扩展的ListView {

/ **
 * @参数方面
 * /
公共ClippedListView(上下文的背景下){
    超(上下文);
}

/ **
 * @参数方面
 * @参数ATTRS
 * /
公共ClippedListView(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
}

@覆盖
保护无效dispatchDraw(帆布油画){
    浮半径= 10.0f;
    路径clipPath =新路径();
    RectF矩形=新RectF(0,0,this.getWidth(),this.getHeight());
    clipPath.addRoundRect(矩形,半径,半径,Path.Direction.CW);
    canvas.clipPath(clipPath);
    super.dispatchDraw(画布);
}
}
 

首先裁剪不能正常工作,然后用

  

setLayerType(View.LAYER_TYPE_SOFTWARE,空)

在我clippedListView解决问题。我的项目的背景是不符合我的角落搞乱了!

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?

解决方案

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!