如何修改ListView的单击样式

时间:2012-08-24 09:52:19

标签: android listview

我使用下面的代码来修改ListView项目点击的样式 list_four_corner_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:state_pressed="true" 
        android:drawable="@drawable/list_four_corner_selected" 
    />
    <item
        android:drawable="@drawable/list_four_corner_unselect" 
    />
</selector>

list_four_corner_unselect.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:startColor="#FFFFFF"
        android:endColor="#FFFFFF"
        android:angle="270"/>
    <corners android:bottomLeftRadius="10dip"
        android:bottomRightRadius="10dip"
        android:topRightRadius="10dip"
        android:topLeftRadius="10dip" />
    <stroke
        android:width="1dp"
        android:color="#dddddd"
        android:dashWidth="10dp"
        android:dashGap="0dp" />
</shape> 

list_four_corner_selected.xml

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

以下BaseAdapter中的代码:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewTag viewTag;
    if(convertView == null) {
        convertView = myInflater.inflate(R.layout.row_item, null);
        viewTag = new ViewTag((TextView)convertView.findViewById(R.id.row_title));
        convertView.setTag(viewTag);
    }
    else {
        viewTag = (ViewTag)convertView.getTag();
    }
    convertView.setBackgroundResource(R.drawable.list_four_corner_selector);
}

但是当我点击项目时,项目的四个角将显示默认颜色 我想隐藏默认值。
我该怎么做?

1 个答案:

答案 0 :(得分:2)

尝试使用ListView上的android:listSelector