我有一个带有singleChoice模式的listView,在我选择一个项目之后滚动列表有时候选择器会悬停在不同的项目上。我有多种行类型布局,但我不认为这是问题。我怎么能阻止这种情况发生?感谢
RoomDeviceListAdapter extends ArrayAdapter<Object> {
private static final int DEFAULT_LAYOUT = -1;
private static final int SECTION_LAYOUT = 100;
private static final int RELAY_LAYOUT = 0;
private static final int DIMMER_LAYOUT = 1;
private static final int HVAC_LAYOUT = 2;
private static final int HEATING_LAYOUT = 3;
private static final int BLIND_LAYOUT = 4;
private static final int RGB_LAYOUT = 5;
private static final int _4Z_LAYOUT = 6;
private static final int _4T_LAYOUT = 7;
private static final int _Z_AUDIO_LAYOUT = 8;
public List<Object> mDevices = new ArrayList<Object>();
private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
private final LayoutInflater mInflater;
protected Context mContext;
public RoomDeviceListAdapter(Context context) {
super(context, R.layout.row_device_relay);
mContext = context;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final Object item) {
mDevices.add(item);
notifyDataSetChanged();
}
public void addSectionHeaderItem(final Object item) {
mDevices.add(item);
sectionHeader.add(mDevices.size() - 1);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mDevices.size();
}
@Override
public Object getItem(int i) {
return mDevices.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public int getItemViewType(int position) {
if(sectionHeader.contains(position))
return SECTION_LAYOUT;
if(((BaseDevice)mDevices.get(position)).getKey().equalsIgnoreCase(AppGroup.RELAY_KEY))
return RELAY_LAYOUT;
if(((BaseDevice)mDevices.get(position)).getKey().equalsIgnoreCase(AppGroup.RGB_KEY))
return RGB_LAYOUT;
if(((BaseDevice)mDevices.get(position)).getKey().equalsIgnoreCase(AppGroup.DIMMER_KEY))
return DIMMER_LAYOUT;
if(((BaseDevice)mDevices.get(position)).getKey().equalsIgnoreCase(AppGroup.HVAC_KEY))
return HVAC_LAYOUT;
if(((BaseDevice)mDevices.get(position)).getKey().equalsIgnoreCase(AppGroup.HEATING_KEY))
return HEATING_LAYOUT;
if(((BaseDevice)mDevices.get(position)).getKey().equalsIgnoreCase(AppGroup.BLIND_KEY))
return BLIND_LAYOUT;
if(((BaseDevice)mDevices.get(position)).getKey().equalsIgnoreCase(AppGroup._4Z_KEY))
return _4Z_LAYOUT;
if(((BaseDevice)mDevices.get(position)).getKey().equalsIgnoreCase(AppGroup._4T_KEY))
return _4T_LAYOUT;
if(((BaseDevice)mDevices.get(position)).getKey().equalsIgnoreCase(AppGroup.BLIND_KEY))
return BLIND_LAYOUT;
if(((BaseDevice)mDevices.get(position)).getKey().equalsIgnoreCase(AppGroup._Z_AUDIO_KEY))
return _Z_AUDIO_LAYOUT;
return RELAY_LAYOUT;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
holder = new ViewHolder();
final Object device = mDevices.get(position);
switch (getItemViewType(position)) {
case SECTION_LAYOUT:
convertView = mInflater.inflate(R.layout.row_device_list_header, parent, false);
holder.sectionName = (TextView) convertView.findViewById(R.id.sectionName);
holder.sectionPowerButton = (ToggleButton) convertView.findViewById(R.id.sectionPower);
holder.sectionName.setText(DeviceUtils.devicesDefaultNames.get(((BaseDevice) device).getKey()));
setState(holder, SECTION_LAYOUT, computeSectionState(((BaseDevice) device).getKey()));
holder.sectionPowerButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mContext instanceof DeviceListAdapterCallback) {
if (isChecked) {
((DeviceListAdapterCallback) mContext).setSectionState(position, ((BaseDevice) device).getKey(), IState.ACTIVE_STATE);
} else {
((DeviceListAdapterCallback) mContext).setSectionState(position, ((BaseDevice) device).getKey(), IState.INACTIVE_STATE);
}
}
}
});
break;
case RELAY_LAYOUT :
convertView = mInflater.inflate(R.layout.row_device_relay, parent, false);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.deviceName = (TextView) convertView.findViewById(R.id.deviceName);
holder.powerButton = (ToggleButton) convertView.findViewById(R.id.powerButton);
if(device.getClass() == Relay.class) {
holder.icon.setImageDrawable(mContext.getResources().getDrawable(((Relay)device).getIcon()));
holder.deviceName.setText(((Relay) device).getName());
setState(holder, RELAY_LAYOUT, ((Relay)device).getState());
}
holder.powerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mContext instanceof DeviceListAdapterCallback) {
if (holder.powerButton.isChecked()) {
((DeviceListAdapterCallback) mContext).setState(((BaseDevice) device).getId(), IState.ACTIVE_STATE);
} else {
((DeviceListAdapterCallback) mContext).setState(((BaseDevice) device).getId(), IState.INACTIVE_STATE);
}
}
}
});
break;
case RGB_LAYOUT :
convertView = mInflater.inflate(R.layout.row_device_rgb, parent, false);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.deviceName = (TextView) convertView.findViewById(R.id.deviceName);
holder.powerButton = (ToggleButton) convertView.findViewById(R.id.powerButton);
if(device.getClass() == RGB.class) {
holder.icon.setImageDrawable(mContext.getResources().getDrawable(((RGB)device).getIcon()));
holder.deviceName.setText(((RGB) device).getName());
setState(holder, RGB_LAYOUT, ((RGB)device).getState());
}
holder.powerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mContext instanceof DeviceListAdapterCallback) {
if (holder.powerButton.isChecked()) {
((DeviceListAdapterCallback) mContext).setState(((BaseDevice) device).getId(), IState.ACTIVE_STATE);
} else {
((DeviceListAdapterCallback) mContext).setState(((BaseDevice) device).getId(), IState.INACTIVE_STATE);
}
}
}
});
break;
case DIMMER_LAYOUT :
convertView = mInflater.inflate(R.layout.row_device_dimmer, parent, false);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.deviceName = (TextView) convertView.findViewById(R.id.deviceName);
holder.modeIcon = (ImageView) convertView.findViewById(R.id.modeIcon);
holder.modeValue = (TextView) convertView.findViewById(R.id.modeValue);
holder.powerButton = (ToggleButton) convertView.findViewById(R.id.powerButton);
if(device.getClass() == Dimmer.class) {
holder.icon.setImageDrawable(mContext.getResources().getDrawable(((Dimmer) device).getIcon()));
holder.deviceName.setText(((Dimmer) device).getName());
holder.modeValue.setText(((Dimmer) device).formatPercentage(mContext));
setState(holder, DIMMER_LAYOUT, ((Dimmer)device).getState());
}
holder.powerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mContext instanceof DeviceListAdapterCallback) {
if(holder.powerButton.isChecked()) {
((DeviceListAdapterCallback) mContext).setState(((BaseDevice) device).getId(), IState.ACTIVE_STATE, 100);
} else {
((DeviceListAdapterCallback) mContext).setState(((BaseDevice) device).getId(), IState.INACTIVE_STATE, 0);
}
}
}
});
break;
case HVAC_LAYOUT :
convertView = mInflater.inflate(R.layout.row_device_hvac, parent, false);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.modeIcon = (ImageView) convertView.findViewById(R.id.modeIcon);
holder.fanIcon = (ImageView) convertView.findViewById(R.id.fanIcon);
holder.deviceName = (TextView) convertView.findViewById(R.id.deviceName);
holder.modeValue = (TextView) convertView.findViewById(R.id.modeValue);
holder.fanValue = (TextView) convertView.findViewById(R.id.fanValue);
holder.powerButton = (ToggleButton) convertView.findViewById(R.id.powerButton);
if(device.getClass() == HVAC.class) {
holder.icon.setImageDrawable(mContext.getResources().getDrawable(((HVAC) device).getIcon()));
holder.modeIcon.setImageDrawable(mContext.getResources().getDrawable(((HVAC) device).getMode().getIcon()));
holder.modeValue.setText((((HVAC) device).getMode().formatValue(mContext)));
if(((HVAC)device).getMode().getType() == ModuleModes.FAN_MODE) {
holder.fanIcon.setVisibility(View.GONE);
holder.fanValue.setVisibility(View.GONE);
} else {
holder.fanIcon.setVisibility(View.VISIBLE);
holder.fanValue.setVisibility(View.VISIBLE);
holder.fanIcon.setImageDrawable(mContext.getResources().getDrawable(((HVAC) device).getFanSpeed().getIcon()));
holder.fanValue.setText((((HVAC) device).getFanSpeed().formatFanSpeed(true)));
}
holder.deviceName.setText(((HVAC)device).getName());
setState(holder, HVAC_LAYOUT, ((HVAC) device).getState());
}
holder.powerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mContext instanceof DeviceListAdapterCallback) {
if (holder.powerButton.isChecked()) {
((DeviceListAdapterCallback) mContext).setState(((BaseDevice) device).getId(), IState.ACTIVE_STATE);
} else {
((DeviceListAdapterCallback) mContext).setState(((BaseDevice) device).getId(), IState.INACTIVE_STATE);
}
}
}
});
break;
case HEATING_LAYOUT :
convertView = mInflater.inflate(R.layout.row_device_heating, parent, false);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.deviceName = (TextView) convertView.findViewById(R.id.deviceName);
holder.powerButton = (ToggleButton) convertView.findViewById(R.id.powerButton);
holder.modeIcon = (ImageView) convertView.findViewById(R.id.modeIcon);
holder.modeValue = (TextView) convertView.findViewById(R.id.modeValue);
if(device.getClass() == Heating.class) {
holder.icon.setImageDrawable(mContext.getResources().getDrawable(((Heating)device).getIcon()));
holder.deviceName.setText(((Heating) device).getName());
holder.modeIcon.setImageDrawable(mContext.getResources().getDrawable(((Heating) device).getMode().getIcon()));
holder.modeValue.setText((((Heating) device).getMode().formatValue(mContext)));
setState(holder, HEATING_LAYOUT, ((Heating) device).getState());
}
holder.powerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mContext instanceof DeviceListAdapterCallback) {
if (holder.powerButton.isChecked()) {
((DeviceListAdapterCallback) mContext).setState(((BaseDevice) device).getId(), IState.ACTIVE_STATE);
} else {
((DeviceListAdapterCallback) mContext).setState(((BaseDevice) device).getId(), IState.INACTIVE_STATE);
}
}
}
});
break;
case BLIND_LAYOUT :
convertView = mInflater.inflate(R.layout.row_device_blinds, parent, false);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.deviceName = (TextView) convertView.findViewById(R.id.deviceName);
holder.modeIcon = (ImageView) convertView.findViewById(R.id.modeIcon);
holder.modeValue = (TextView) convertView.findViewById(R.id.modeValue);
holder.blindsOpenButton = (ToggleButton) convertView.findViewById(R.id.blindsOn);
holder.blindsCloseButton = (ToggleButton) convertView.findViewById(R.id.blindsOff);
if(device.getClass() == Blind.class) {
holder.icon.setImageDrawable(mContext.getResources().getDrawable(((Blind) device).getIcon()));
holder.deviceName.setText(((Blind) device).getName());
holder.modeIcon.setImageDrawable(mContext.getResources().getDrawable(((Blind) device).getMode().getIcon()));
holder.modeValue.setText(((Blind) device).getMode().getName());
holder.modeIcon.setImageDrawable(mContext.getResources().getDrawable(((Blind) device).getMode().getIcon()));
holder.modeValue.setText(((Blind) device).getMode().getName());
setState(holder, BLIND_LAYOUT, ((Blind) device).getState(), ((Blind) device).getMode());
}
holder.blindsOpenButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mContext instanceof DeviceListAdapterCallback) {
if(holder.blindsCloseButton.isChecked()) {
holder.blindsCloseButton.setChecked(false);
}
if(isChecked) {
((DeviceListAdapterCallback) mContext).setState(((BaseDevice) device).getId(), IState.ACTIVE_STATE, DeviceModeLists.getModeByType(((Blind) device).getDeviceModes(), ModuleModes.BLINDS_OPEN_MODE));
} else {
((DeviceListAdapterCallback) mContext).setState(((BaseDevice) device).getId(), IState.INACTIVE_STATE, DeviceModeLists.getModeByType(((Blind)device).getDeviceModes(), ModuleModes.BLINDS_OPEN_MODE));
}
}
}
});
holder.blindsCloseButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mContext instanceof DeviceListAdapterCallback) {
if (holder.blindsOpenButton.isChecked()) {
holder.blindsOpenButton.setChecked(false);
}
if (isChecked) {
((DeviceListAdapterCallback) mContext).setState(((BaseDevice) device).getId(), IState.ACTIVE_STATE, DeviceModeLists.getModeByType(((Blind) device).getDeviceModes(), ModuleModes.BLINDS_CLOSE_MODE));
} else {
((DeviceListAdapterCallback) mContext).setState(((BaseDevice) device).getId(), IState.INACTIVE_STATE, DeviceModeLists.getModeByType(((Blind) device).getDeviceModes(), ModuleModes.BLINDS_CLOSE_MODE));
}
}
}
});
break;
case _4Z_LAYOUT:
convertView = mInflater.inflate(R.layout.row_device_4z, parent, false);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.deviceName = (TextView) convertView.findViewById(R.id.deviceName);
holder.status = (ImageView) convertView.findViewById(R.id.status);
if(device.getClass() == _4Z.class) {
holder.icon.setImageDrawable(mContext.getResources().getDrawable(((BaseDevice)device).getIcon()));
holder.deviceName.setText(((BaseDevice) device).getName());
setState(holder, _4Z_LAYOUT, ((BaseDevice) device).getState());
if(((_4Z)device).isHasError()) {
holder.status.setImageDrawable(mContext.getResources().getDrawable(R.drawable.atentie));
} else {
holder.status.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ok));
}
}
break;
case _4T_LAYOUT:
convertView = mInflater.inflate(R.layout.row_device_4t, parent, false);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.deviceName = (TextView) convertView.findViewById(R.id.deviceName);
holder.modeValue = (TextView) convertView.findViewById(R.id.modeValue);
holder.modeIcon = (ImageView) convertView.findViewById(R.id.modeIcon);
holder.status = (ImageView) convertView.findViewById(R.id.status);
if(device.getClass() == _4T.class) {
holder.icon.setImageDrawable(mContext.getResources().getDrawable(((BaseDevice)device).getIcon()));
holder.deviceName.setText(((BaseDevice) device).getName());
holder.modeValue.setText(String.valueOf(((_4T) device).getValue()));
setState(holder, _4T_LAYOUT, ((BaseDevice) device).getState());
if(((_4T)device).isHasError()) {
holder.status.setImageDrawable(mContext.getResources().getDrawable(R.drawable.atentie));
} else {
holder.status.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ok));
}
}
break;
case _Z_AUDIO_LAYOUT:
convertView = mInflater.inflate(R.layout.row_z_audio, parent, false);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.deviceName = (TextView) convertView.findViewById(R.id.deviceName);
if(device.getClass() == _zAudio.class) {
holder.icon.setImageDrawable(mContext.getResources().getDrawable(((BaseDevice)device).getIcon()));
holder.deviceName.setText(((BaseDevice) device).getName());
setState(holder, _Z_AUDIO_LAYOUT, ((BaseDevice) device).getState());
}
break;
default:
convertView = mInflater.inflate(R.layout.row_device_relay, parent, false);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.deviceName = (TextView) convertView.findViewById(R.id.deviceName);
holder.powerButton = (ToggleButton) convertView.findViewById(R.id.powerButton);
if(device.getClass() == BaseDevice.class) {
holder.icon.setImageDrawable(mContext.getResources().getDrawable(((BaseDevice)device).getIcon()));
holder.deviceName.setText(((BaseDevice) device).getName());
setState(holder, DEFAULT_LAYOUT, ((BaseDevice) device).getState());
}
holder.powerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mContext instanceof DeviceListAdapterCallback) {
if (holder.powerButton.isChecked()) {
((DeviceListAdapterCallback) mContext).setState(((BaseDevice) device).getId(), IState.ACTIVE_STATE);
} else {
((DeviceListAdapterCallback) mContext).setState(((BaseDevice) device).getId(), IState.INACTIVE_STATE);
}
}
}
});
break;
}
return convertView;
}
static class ViewHolder {
ImageView icon;
TextView deviceName;
ImageView modeIcon;
TextView modeValue;
ImageView fanIcon;
ImageView status;
TextView fanValue;
ToggleButton powerButton;
ToggleButton blindsOpenButton;
ToggleButton blindsCloseButton;
TextView sectionName;
ToggleButton sectionPowerButton;
}
}