Android 视图保持架模式不工作
我已经实现了一个自定义列表视图,其中有两个文本视图和复选框。在whoms上,单击我选中/取消选中复选框,这些将封装到线性布局中。我面临的问题是,当我单击线性布局时,它会选中复选框,但也会选中固定模式上的其他复选框,即每隔7-8行。我读了很多关于它的问题,发现实现一个视图持有者可以解决这个问题,但这对我来说也不起作用。以下是我的代码: CustomListAdapter:Android 视图保持架模式不工作,android,listview,checkbox,Android,Listview,Checkbox,我已经实现了一个自定义列表视图,其中有两个文本视图和复选框。在whoms上,单击我选中/取消选中复选框,这些将封装到线性布局中。我面临的问题是,当我单击线性布局时,它会选中复选框,但也会选中固定模式上的其他复选框,即每隔7-8行。我读了很多关于它的问题,发现实现一个视图持有者可以解决这个问题,但这对我来说也不起作用。以下是我的代码: CustomListAdapter: public class ContactsListAdapter extends BaseAdapter{ priv
public class ContactsListAdapter extends BaseAdapter{
private Context context;
private List<ContactInfo> contacts;
private List<ContactInfo> selectedContacts;
public ContactsListAdapter(Context context, List<ContactInfo> contacts) {
this.context = context;
this.contacts = contacts;
selectedContacts = new ArrayList<ContactInfo>();
}
public List<ContactInfo> getSelectedContacts() {
return selectedContacts;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return contacts.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return contacts.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return Long.parseLong(contacts.get(position).id);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ViewHolder holder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.contacts_list_row_item, null);
holder = new ViewHolder();
holder.view = (LinearLayout) convertView.findViewById(R.id.llContactRow);
holder.name = (TextView) convertView.findViewById(R.id.tvContactName);
holder.number = (TextView) convertView.findViewById(R.id.tvContactNumber);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.cbMultipleSelect);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(contacts.get(position).name);
holder.number.setText(contacts.get(position).number);
holder.view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ContactInfo contact = new ContactInfo();
contact.id = contacts.get(position).id;
contact.name = contacts.get(position).name;
contact.number = contacts.get(position).number;
holder.checkbox.setChecked(!holder.checkbox.isChecked());
if(holder.checkbox.isChecked()) {
selectedContacts.add(contact);
} else {
selectedContacts.remove(contact);
}
}
});
return convertView;
}
private static class ViewHolder {
LinearLayout view;
CheckBox checkbox;
TextView name;
TextView number;
}
}
您的问题是需要将holder.checkbox.setChecked()设置为行下方的值:
holder.number.setText(contacts.get(position).number);
当前,在选中它时,您正在更改它的值,但是在调用getview方法时,您需要设置该值,否则它将只保留以前的值
如果您的contactinfo中有一个“选定”值,而不是管理一个新列表,这可能是最好的。这样,您可以执行以下操作:
holder.checkbox.setChecked(contacts.get(position).isSelected);
您没有在
getView()
中显示您的内容。请执行以下操作:
@Override
public void onClick(View v) {
//remove the other stuff. You don't want to create new instances here, otherwise you won't be able to remove them later
holder.checkbox.setChecked(!holder.checkbox.isChecked());
if(holder.checkbox.isChecked()) {
selectedContacts.add(contact.get(position));
} else {
selectedContacts.remove(contact.get(position));
}
}
在为您的线性布局设置OnClickListener
之前执行此操作:
holder.checkbox.setChecked(selectedContacts.contains(contacts.get(position)));
如果您希望在点击复选框时有相同的行为,只需在复选框中添加以下内容:
android:clickable="false"
因此,linearLayout
可以拦截它,但我希望只有当用户单击线性布局时才选中它为什么最好?问题是复选框没有被重置。也许您只是想确保setChecked设置为false,以便在单击后重置并仅检查它。您正在不必要地管理另一个数组。跟踪数据比从另一个数组中添加/删除数据更容易。您的意思是说在ContactInfo类中添加另一个布尔值,如果选择或不选择联系人,该布尔值将保留该值。在类中添加一个不相关的布尔值不是一个好主意。问题仍然存在。实际上它起了作用。但问题是过了一段时间,检查消失了(当我滚动列表视图时)。你是怎么做到的?你能更新吗?这对我来说很好实际上我错过了你最后一行的答案。当我把它添加到我的代码中时,它起了作用。然后我滚动,支票消失了。不,支票消失了。为什么会这样?
android:clickable="false"