适配器上的Android更改复选框无法正常工作
在我的简单适配器中,我想管理项目上的复选框。当我点击一个复选框时,我改变了一个状态,当我滚动复选框时,状态为false,我无法修复我的代码来解决这个问题适配器上的Android更改复选框无法正常工作,android,Android,在我的简单适配器中,我想管理项目上的复选框。当我点击一个复选框时,我改变了一个状态,当我滚动复选框时,状态为false,我无法修复我的代码来解决这个问题 public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.CustomContactsViewHolder> { private OnCardClickListener onCardClickListener; private
public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.CustomContactsViewHolder> {
private OnCardClickListener onCardClickListener;
private List<UserPhoneContacts> list = Collections.emptyList();
private Context context;
private Realm realm;
public static OnSelectedContacts onSelectedContacts;
private Map<String, Boolean> checkBoxStates = new HashMap<>();
public ContactsAdapter(List<UserPhoneContacts> list, Context context) {
this.list = list;
this.context = context;
this.realm = Realm.getDefaultInstance();
}
@Override
public CustomContactsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.contact_item, parent, false);
CustomContactsViewHolder holder = new CustomContactsViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(final CustomContactsViewHolder holder, final int position) {
holder.contact_name.setText(list.get(position).getDisplayName());
holder.contact_mobile.setText(list.get(position).getMobileNumber());
Boolean checkedState = checkBoxStates.get(list.get(position).getMobileNumber());
holder.select_contact.setChecked(checkedState == null ? false : checkedState);
holder.select_contact.setTag(position);
holder.select_contact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onChange((Integer) v.getTag());
}
});
}
private void onChange(int position) {
final UserPhoneContacts item = list.get(position);
if (item == null) {
return;
}
boolean checkedState = checkBoxStates.get(list.get(position).getMobileNumber()) != null;
checkBoxStates.put(item.getMobileNumber(), !checkedState);
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public void setData(List<UserPhoneContacts> list) {
this.list = list;
}
public static class CustomContactsViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.contact_name)
TextView contact_name;
@BindView(R.id.contact_mobile)
TextView contact_mobile;
@BindView(R.id.contact_photo)
CircleImageView contact_photo;
@BindView(R.id.select_contact)
CheckBox select_contact;
CustomContactsViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
}
公共类ContactsAdapter扩展了RecyclerView.Adapter{
私有OnCardClickListener OnCardClickListener;
私有列表=Collections.emptyList();
私人语境;
私人领域;
公共静态On Selected联系人On Selected联系人;
私有映射checkBoxStates=newhashmap();
公共联系人适配器(列表、上下文){
this.list=列表;
this.context=上下文;
this.realm=realm.getDefaultInstance();
}
@凌驾
public CustomContactsViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.contact_项,parent,false);
CustomContactsViewHolder holder=新的CustomContactsViewHolder(v);
报税表持有人;
}
@凌驾
BindViewHolder上的公共无效(最终客户联系人ViewHolder持有人,最终int位置){
holder.contact_name.setText(list.get(position.getDisplayName());
holder.contact_mobile.setText(list.get(position.getMobileNumber());
Boolean checkedState=checkBoxStates.get(list.get(position.getMobileNumber());
holder.select_contact.setChecked(checkedState==null?false:checkedState);
支架。选择触点。设置标签(位置);
holder.select\u contact.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
onChange((整数)v.getTag());
}
});
}
私有void onChange(内部位置){
最终UserPhoneContacts项=list.get(位置);
如果(项==null){
返回;
}
boolean checkedState=checkBoxStates.get(list.get(position).getMobileNumber())!=null;
checkBoxStates.put(item.getMobileNumber(),!checkedState);
notifyDataSetChanged();
}
@凌驾
public int getItemCount(){
返回list.size();
}
@凌驾
附加ToRecyclerView(RecyclerView RecyclerView)上的公共无效{
super.onAttachedToRecyclerView(recyclerView);
}
公共无效设置数据(列表){
this.list=列表;
}
公共静态类CustomContactsViewHolder扩展了RecyclerView.ViewHolder{
@BindView(R.id.contact_name)
TextView联系人姓名;
@BindView(R.id.contact_mobile)
TextView联系人(手机),;
@BindView(R.id.contact_照片)
CircleImageView联系人照片;
@BindView(R.id.select\u联系人)
复选框选择_联系人;
自定义联系人查看文件夹(视图){
超级(视图);
ButterKnife.bind(这个,视图);
}
}
}
当我点击复选框时会发生什么
单击->选中,单击->未选中,单击->未选中,单击->未选中,单击->未选中
在取消选中复选框之后,我无法通过单击该复选框来更改复选框状态。在这里,我将您的CustomContactsViewHolder类设置为非静态类,并对单击事件进行了一些更改,并将数据再次设置为选中和取消选中,一旦使用完整代码进行检查,则将代码作为备份
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.CustomContactsViewHolder> {
private OnCardClickListener onCardClickListener;
private List<UserPhoneContacts> list = Collections.emptyList();
private Context context;
private Realm realm;
public static OnSelectedContacts onSelectedContacts;
private Map<String, Boolean> checkBoxStates = new HashMap<>();
public ContactsAdapter(List<UserPhoneContacts> list, Context context) {
this.list = list;
this.context = context;
this.realm = Realm.getDefaultInstance();
}
@Override
public CustomContactsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.contact_item, parent, false);
CustomContactsViewHolder holder = new CustomContactsViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(final CustomContactsViewHolder holder, final int position) {
holder.contact_name.setText(list.get(position).getDisplayName());
holder.contact_mobile.setText(list.get(position).getMobileNumber());
boolean checkedState = checkBoxStates.containsKey(list.get(position).getMobileNumber())?checkBoxStates.get(list.get(position).getMobileNumber()):false;
holder.select_contact.setChecked(checkedState);
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public void setData(List<UserPhoneContacts> list) {
this.list = list;
}
public class CustomContactsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
@BindView(R.id.contact_name)
TextView contact_name;
@BindView(R.id.contact_mobile)
TextView contact_mobile;
@BindView(R.id.contact_photo)
CircleImageView contact_photo;
@BindView(R.id.select_contact)
CheckBox select_contact;
CustomContactsViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
@OnClick(R.id.select_contact)
@Override
public void onClick(View v) {
int position = getAdapterPosition();
switch (v.getId()){
case R.id.select_contact:
final UserPhoneContacts item = list.get(position);
if (item == null) {
return;
}
boolean checkedState = checkBoxStates.containsKey(list.get(position).getMobileNumber())?checkBoxStates.get(list.get(position).getMobileNumber()):false;
((CheckBox)v).setChecked(!checkedState);
checkBoxStates.put(item.getMobileNumber(), !checkedState);
ContactsAdapter.this.notifyDataSetChanged();
break;
}
}
}
}
导入android.content.Context;
导入android.support.v7.widget.RecyclerView;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.CheckBox;
导入android.widget.TextView;
导入java.util.Collections;
导入java.util.HashMap;
导入java.util.Map;
公共类ContactsAdapter扩展了RecyclerView.Adapter{
私有OnCardClickListener OnCardClickListener;
私有列表=Collections.emptyList();
私人语境;
私人领域;
公共静态On Selected联系人On Selected联系人;
私有映射checkBoxStates=newhashmap();
公共联系人适配器(列表、上下文){
this.list=列表;
this.context=上下文;
this.realm=realm.getDefaultInstance();
}
@凌驾
public CustomContactsViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.contact_项,parent,false);
CustomContactsViewHolder holder=新的CustomContactsViewHolder(v);
报税表持有人;
}
@凌驾
BindViewHolder上的公共无效(最终客户联系人ViewHolder持有人,最终int位置){
holder.contact_name.setText(list.get(position.getDisplayName());
holder.contact_mobile.setText(list.get(position.getMobileNumber());
boolean checkedState=CheckboxState.containsKey(list.get(position).getMobileNumber())?CheckboxState.get(list.get(position).getMobileNumber()):false;
保持架。选择_contact.setChecked(选中状态);
}
@凌驾
public int getItemCount(){
返回list.size();
}
@凌驾
附加ToRecyclerView(RecyclerView RecyclerView)上的公共无效{
super.onAttachedToRecyclerView(recyclerView);
}
公共无效设置数据(列表){
this.list=列表;
}
公共类CustomContactsViewHolder扩展了RecyclerView.ViewHolder实现了View.OnClickListener{
@BindView(R.id.contact_name)
TextView联系人姓名;
@BindView(R.id.contact_mobile)
TextView联系人(手机),;
@BindView(R.id.contact_照片)
CircleImageView联系人照片;
@BindView(R.id.select\u联系人)
复选框选择_联系人;
自定义联系人查看文件夹(视图){
超级(视图);
ButterKnife.绑定(此,视图)
public class UserPhoneContacts {
// other variables
private boolean isSelected = false; // add this
// your class constructor and other getter/setter methods
// add below getter method
public boolean isSelected() {
return this.isSelected;
}
// add below setter method
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
}
@Override
public void onBindViewHolder(final CustomContactsViewHolder holder, final int position) {
...
final UserPhoneContacts upc = list.get(position);
holder.select_contact.setChecked(upc.isSelected() ? true : false);
holder.select_contact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
upc.setSelected(!upc.isSelected());
holder.view.setChecked(upc.isSelected() ? true : false);
}
});
}
private void onChange(int position) {
final UserPhoneContacts item = list.get(position);
if (item == null) {
return;
}
boolean checkedState = checkBoxStates.get(list.get(position).getMobileNumber()) != null;
checkBoxStates.put(item.getMobileNumber(), !checkedState);
notifyDataSetChanged();
}
private void onChange(int position) {
final UserPhoneContacts item = list.get(position);
if (item == null) {
return;
}
Boolean lastCheckedState = checkBoxStates.get(list.get(position).getMobileNumber());
boolean checkedState = (null == lastCheckedState) ? false : lastCheckedState;
checkBoxStates.put(item.getMobileNumber(), !checkedState);
notifyDataSetChanged();
}