我想创建一个单项自定义视图,并且我有以下代码,但它不起作用。它检查多个复选框。有人可以帮我解决这个问题:
public class SingleChoiceRelativeLayout extends RelativeLayout implements Checkable{
private CheckBox checkableView;
public SingleChoiceRelativeLayout(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public SingleChoiceRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SingleChoiceRelativeLayout(Context context) {
super(context);
}
public CheckBox getCheckableView() {
return checkableView;
}
public void setCheckableView(CheckBox checkableView) {
this.checkableView = checkableView;
}
@Override
public void setChecked(boolean checked) {
if (this.checkableView != null){
this.checkableView.setChecked(checked);
}
}
@Override
public boolean isChecked() {
if (this.checkableView != null){
this.checkableView.isChecked());
return this.checkableView.isChecked();
}
return false;
}
@Override
public void toggle() {
if (this.checkableView != null){
this.checkableView.setChecked(!this.checkableView.isChecked());
}
}
}
<com.xyz.view.SingleChoiceRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="47.5dip" >
<TextView
android:id="@+id/device_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="24dip"
android:text="Group 1"
android:textColor="#393c3d"
android:textSize="16sp" />
<CheckBox
android:id="@+id/tickmark_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@android:color/transparent"
android:button="@drawable/tick_mark_icon_state_drawable"
android:checked="false"
android:padding="10dip" />
<!-- <View -->
<!-- android:layout_width="match_parent" -->
<!-- android:layout_height="0.5dip" -->
<!-- android:layout_alignParentBottom="true" -->
<!-- android:background="@color/action_bar_divider_line" /> -->
</com.xyz.view.SingleChoiceRelativeLayout>
<ListView android:id="@+id/devices_list_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="17dip" android:layout_marginRight="17dip" android:layout_marginBottom="17dip" android:divider="@color/action_bar_divider_line" android:layout_below="@id/buttons_orbit" android:choiceMode="singleChoice" android:dividerHeight="1dip" android:scrollbars="none"/>
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ItemViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Inflate the vertical list item
convertView = inflater.inflate(R.layout.list_item_devices_layout, parent, false);
viewHolder = new ItemViewHolder();
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.tickmark_view);
viewHolder.checkBox.setOnCheckedChangeListener(new OnDeviceSelectionChangedListener());
((SingleChoiceRelativeLayout)convertView).setCheckableView(viewHolder.checkBox);
viewHolder.deviceName = (TextView) convertView.findViewById(R.id.device_name);
convertView.setTag(viewHolder);
} else {
viewHolder = (ItemViewHolder) convertView.getTag();
}
setUpListItemView(viewHolder, position);
return convertView;
}
adapter = new DevicesListAdapter(DevicesActivity.this, devices);
ListView list = (ListView)findViewById(R.id.devices_list_view);
list.setSelection(ListView.CHOICE_MODE_SINGLE);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
});
list.setAdapter(adapter);
private void setUpListItemView(ItemViewHolder viewHolder, int position){
viewHolder.deviceName.setText(devicesList.get(position).getDeviceName());
}