我想拥有一个只有一个选择的列表视图。我使用CheckedTextView,当用户点击一行时,我更新了CheckedTextView。如果他点击另一行,旧选择将检索" normal" UI和新选择采用新UI。
它在nexus 7(4.2.2)上工作正常,但在galaxy S II(4.0.4)上,我有一个案例,我选择了两行,我必须点击两次才能有正确的行为。
我确切地说,我已经设置了这个属性:
setChoiceMode(ListView.CHOICE_MODE_SINGLE);
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.ecab"
android:id="@+id/option"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/ban_vhcl_list_off"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/fake" />
这是我的适配器
private class OptionsAdapter extends ArrayAdapter<String> implements OnItemClickListener {
private final List<String> options;
public OptionsAdapter(List<String> pOptions) {
super(VehiculeActivity.this, R.layout.vehicule_list_item);
options = pOptions;
}
@Override
public int getCount() {
return options.size();
}
@Override
public String getItem(int position) {
return options.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = LayoutInflater.from(VehiculeActivity.this).inflate(R.layout.vehicule_list_item, null);
CheckedTextView option = (CheckedTextView) convertView.findViewById(R.id.option);
option.setText(options.get(position));
if (!option.isChecked()) {
Log.d(Constants.D_TAG, "is NOT checked");
option.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
option.setBackgroundResource(R.drawable.ban_vhcl_list_off);
} else {
Log.d(Constants.D_TAG, "is checked");
option.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.check_box, 0);
option.setBackgroundResource(R.drawable.ban_vhcl_list_on);
}
return convertView;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView option = (CheckedTextView) view.findViewById(R.id.option);
option.setChecked(true);
notifyDataSetChanged();
}
}