我想要一个带有单一选择的列表视图。我使用 CheckedTextView,当用户单击一行时,我更新 CheckedTextView。如果他单击另一行,则旧选择会检索“正常” 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();
}
}