首页 > 解决方案 > Android RecyclerView 复选框会自行检查

问题描述

我有一个 RecyclerView,它有一个复选框和 textview。数字 10,20,30,40... 直到 500 应该显示在 textview 中。选中的复选框应该在文本视图中添加与复选框相对应的数字。例如。如果用户仅检查值 10,则 textView 将显示 10。如果用户也检查 20,则 TextView 将显示 30 ( 20 +10)。如果用户再次取消选中 10,TextView 将显示 20,依此类推。当我单击复选框时,还会选中一些随机复选框。我在 stackoverflow 中尝试了一种解决方案。它没有用。我被困住了。请帮帮我..这是我的代码:

RecyclerAdapter.java:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerHolder>
    {

        @NonNull
        @Override
        public RecyclerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new RecyclerHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_lyt,parent,false));
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerHolder holder, final int position) {
            holder.number.setText(Integer.toString((Integer) alldata.get(position)));
            final String text=holder.number.getText().toString();


            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Log.e("Checked","Checked");
                    if(checkeddata!=null)
                    {
                        if (checkeddata.size()==0)
                        {
                            checkeddata.add(text);

                        }
                        else {
                            if(checkeddata.contains(text))
                            {
                                checkeddata.remove(text);


                            }
                            else {
                                checkeddata.add(text);

                        }
                        }
                        Iterator iterator=checkeddata.iterator();
                        int sumnumber=0;
                        while (iterator.hasNext())
                        {
                            sumnumber= sumnumber+Integer.parseInt((String) iterator.next());
                        }
                        sum.setText(Integer.toString(sumnumber));
                    }


                }
            });


        }

        @Override
        public int getItemCount() {
            return alldata.size();
        }

        public class RecyclerHolder extends RecyclerView.ViewHolder
        {
            TextView number;
            CheckBox checkBox;
            public RecyclerHolder(View itemView) {
                super(itemView);
                number=itemView.findViewById(R.id.number);
                checkBox=itemView.findViewById(R.id.check);


            }
        }
    }
    public void data()
    {
        int g=1;
        for(int i=10;i<=500;i++)
        {
            if((i/10)==g)
            {
                g=g+1;
                alldata.add(i);
            }

        }
    }

recycler_lyt.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:id="@+id/number"
    android:textSize="20sp"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/check"
        android:checked="false"
        app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>

标签: androidcheckboxandroid-recyclerview

解决方案


You must store your checkbox state. You can do that in your model, for example:

class Model {
    String title;
    boolean checked;
}

Next you must pass List of Model items to your adapter and check/uncheck checkbox determined by your model.

if (listOfItems.get(position).checked) {
    viewHolder.checkbox.setChecked(true);
} else {
    viewHolder.checkbox.setChecked(false);
}

Remember that you always need to manage opposite state of views (like checkbox or visibility of view) in adapter.


推荐阅读