我想创建一个项目自定义视图,我有以下代码,但它不起作用。它会检查多个复选框。请有人帮我解决问题:
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());
}
答案 0 :(得分:0)
在属性上创建以选择索引。
int selectedIndex = -1;
在OnDeviceSelectionChangedListener()
中获取所选项目的位置并将其设置为selectedIndex
,然后调用notifyDatasetChanged()以刷新列表数据
....
selectedIndex = position;
notifyDatasetChanged();
.....
然后在getView中使用此方法
if (position == selectedIndex ) {
viewHolder.checkBox.setChecked(true);
} else {
viewHolder.checkBox.setChecked(false);
}
创建一个静态属性,用于在SingleChoiceRelativeLayout
,
public static int selectedIndex = -1;
然后在onItemClickListener:
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
SingleChoiceRelativeLayout.selectedIndex = position;
adapter.notifyDatasetChanged();
}
});
然后在getView中检查所选项目。
if (position == SingleChoiceRelativeLayout.selectedIndex ) {
viewHolder.checkBox.setChecked(true);
} else {
viewHolder.checkBox.setChecked(false);
}