Android上的Android更改CheckBoxes无法正常工作

时间:2016-11-22 10:25:07

标签: android

在我的简单适配器中,我想管理项目的复选框。当我单击复选框时,我更改状态,当我滚动时,复选框状态为false,我无法修复我的代码来解决此问题

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);
        }
    }
}

点击复选框后会发生什么?

点击 - &gt; 选中,点击 - &gt; 取消选中,点击 - &gt; 未选中,点击 - &gt; 未选中,点击 - &gt;的选中

取消选中复选框后,我无法通过点击

更改复选框状态

3 个答案:

答案 0 :(得分:1)

在这里,我将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;
            }
        }
    }
}

答案 1 :(得分:1)

您无需创建单独的Map<String, Boolean>列表来维护是否选择了CheckBox。尝试下面的简单技巧。但您需要按如下方式更新UserPhoneContacts模型类。

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;
  }
}

像这样更改您的ContactAdapter课程

@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);
        }
    });
}

答案 2 :(得分:0)

问题的主要原因是 onChange 方法。

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();
}

例如,首先点击位置3项checkBoxStates.get(3) return null,因此!checkedState将为真。

其次,您点击位置3,checkBoxStates.get(3) == TruecheckedState = (True != null) = true,因此!checkedState将为false。

第三,您点击位置3,checkBoxStates.get(3) == FalsecheckedState = (False != null) = true,因此!checkedState也将为false,新状态也将取消选中。

修复代码应如下所示:

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();
}