为什么选择列表中的一个项目会选择ListView的所有元素?

时间:2014-05-19 10:31:45

标签: android listview

在Android 2.3上单击列表中的一个项目时,选择整个ListView的颜色与列表中的一个项目相同。我使用自定义选择器。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false"
        android:drawable="@color/transparent" />
    <item android:state_focused="true"
        android:state_enabled="false"
        android:state_pressed="true"
        android:drawable="@drawable/color_press" />
    <item android:state_focused="true"
        android:state_enabled="false" 
        android:drawable="@drawable/color_press" />
    <item android:state_focused="true" 
        android:state_pressed="true"
        android:drawable="@drawable/color_press" />
    <item android:state_focused="false" 
        android:state_pressed="true"
        android:drawable="@drawable/color_press" />
    <item android:state_focused="true" 
        android:drawable="@drawable/color_press" />
</selector>

xml中的ListView。

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="#B7B7B7"
    android:dividerHeight="1px"
    android:listSelector="@drawable/list_view_selector" />

出了什么问题?

UPD:
我的自定义适配器

public class CityAdapter extends ArrayAdapter<CityInfo>
{

    private boolean isRus = false;
    private LayoutInflater inflater;

    public CityAdapter(final Context context, final List<CityInfo> objects)
    {
        super(context, -1, objects);
        isRus = context.getString(R.string.locale).equalsIgnoreCase("ru");
        inflater = LayoutInflater.from(context);
    }

    private class ViewHolder
    {
        RadioButton radioButton;
        TextView textView;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent)
    {
        ViewHolder holder;
        if (convertView == null)
        {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.welcome_city_case_list_item, null);
            holder.radioButton = (RadioButton) convertView.findViewById(R.id.radioButton);
            holder.textView = (TextView) convertView.findViewById(R.id.size);
            convertView.setTag(holder);
        }
        else
            holder = (ViewHolder) convertView.getTag();
        final CityInfo cityInfo = getItem(position);

        holder.radioButton.setText(isRus ? cityInfo.getRus() : cityInfo.getEng());
        holder.textView.setHint(cityInfo.getVisibleZipSize());
        return convertView;
    }


}

welcome_city_case_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:focusable="false"
        android:clickable="false"
        >
    <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingRight="10dp"
            android:textColor="@color/black"
            android:duplicateParentState="true"
            style="@style/CustomRadioButton"
            android:text="Екатеринбург"
            android:layout_weight="1"
            android:paddingTop = "10dp"
            android:paddingBottom = "10dp"
            android:clickable="false"
            android:focusable="false"
            android:background="@color/transparent"
            />
    <TextView
            android:id="@+id/size"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:hint="2,1MB"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:focusable="false"
            />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />

</LinearLayout>

list_selector.xml

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

    <item android:state_pressed="true"
        android:drawable="@drawable/gradient_bg_hover" />

    <item android:state_selected="true"
     android:state_pressed="false"
        android:drawable="@drawable/gradient_bg_hover" />
</selector>

试试这个