Android 更改适配器上的复选框无法正常工作

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

当我点击复选框时会发生什么?

点击->勾选,点击->取消勾选,点击->取消勾选,点击->取消勾选 , 点击-> 未勾选

取消选中复选框后我无法通过单击更改复选框状态

最佳答案

在这里,我将您的 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 更改适配器上的复选框无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40739336/

相关文章:

Android Toolbar SearchView根据其他项目约束宽度

java - Android:无法解析自定义 PagerAdapter 中的方法

android - Gradle 编译选项可以自动影响多模块吗?(dexOptions,aaptOptions,lintOptions)

android - 如何使用 Target API 23 及更高版本通过 GPS 获取用户位置

android - applicationContext recycleview 元素的未解决引用(适用于 android 的 kotlin 应用程序)

java - Android应用程序上的DeadObjectException

android - 在测试类中模拟和监视时获取空指针异常

android - 如何将 Google Play 服务修订版与安装版本匹配?

android - TextInputLayout EditText nextFocusRight 不能正常工作

java - Android java.lang.ClassCastException : android. widget.LinearLayout$LayoutParams 无法转换为 android.widget.AbsListView$LayoutParams