我一直在这个网站和其他网站上查找帖子,但没有找到我需要的工作代码。我目前有一个带有自定义行的列表视图,并且我让它们按颜色交替(灰色和透明)。我试图让选定的行变成蓝色(并在按下另一行时返回原始颜色),即使我滚动离开它也保持选中状态。
编辑:这是完整的列表适配器,减去一些图标的填充方式:
public class MultiActivateAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final List<Model> listInfo;
private final List<Model> listAlert;
private final List<Model> listSecondAlert;
private int _index = -1;
ArrayList<String> selectedStrings = new ArrayList<String>();
private final Activity context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public MultiRenewAdapter2(Activity context, List<Model> list, List<Model> listInfo, List<Model> listAlert, List<Model> listSecondAlert) {
super(context, R.layout.multirenewrow2, list);
this.context = context;
this.list = list;
this.listInfo = listInfo;
this.listAlert = listAlert;
this.listSecondAlert = listSecondAlert;
}
static class ViewHolder {
protected TextView text;
protected ImageView alert;
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflator = context.getLayoutInflater();
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflator.inflate(R.layout.multiactivaterow, null);
viewHolder = new ViewHolder();
viewHolder.alert = (ImageView) convertView.findViewById(R.id.alert);
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
convertView.setTag(viewHolder);
convertView.setTag(R.id.label, viewHolder.text);
convertView.setTag(R.id.check, viewHolder.checkbox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});
if (position == _index)
{
convertView.setBackgroundColor(context.getResources().getColor(R.color.DeepSkyBlue));
}
else
{
convertView.setBackgroundColor(context.getResources().getColor(R.color.transparent));
if (position % 2 == 0)
{
convertView.setBackgroundColor(Color.TRANSPARENT);
}
else
{
convertView.setBackgroundColor(Color.LTGRAY);
}
}
viewHolder.checkbox.setTag(position); // This line is important.
//some code removed to show icon population
viewHolder.text.setText(list.get(position).getName());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
return convertView;
}
public void setClicked(int index)
{
_index = index;
this.notifyDataSetChanged();
}
}
以及适配器曾经所属的旧活动的代码:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
try {
adapt.setClicked(position);
} catch (Exception e) {
System.out.println(e);
}
adapt.notifyDataSetChanged();
....
这段代码曾经适用于我所有的其他 listViews,但由于它使用/依赖于不同的数组和其他东西,我不得不进行更改,这允许我保存显示的选定复选框。