滚动时,在recyclerView中取消选中复选框 [英] checkboxes gets unchecked in recyclerView upon scrolling

查看:127
本文介绍了滚动时,在recyclerView中取消选中复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个光标,它检索成分列表并填充适配器,然后将其分配给具有复选框的回收站视图。我得到的问题是,当我检查一组复选框并向下滚动时,从顶部选择的那些,取消选择,反之亦然,或者在某些情况下,如果我从顶部选择三个,从底部选择三个被选中。我需要确保,最初没有选择任何内容,即使我上下滚动,所选内容也应该保持选中状态并需要跟踪。

I have a cursor which retrieves a list of ingredients and populates an adapter, which is then assigned to a recycler View having checkboxes. The problem I've got is that, when I check a set of checkboxes and scroll down, the ones selected from the top, get deselected and vice versa, or in some instances, if I select three from the top, three from the bottom get selected. I need to make sure that, initially nothing is selected, and even if I scroll up and down, the ones selected should remain selected and need to keep track of.

这里是可以scrolable的列表。

Here is the list which is scrolable.

这是我的代码:

class RecipeDetailCursorAdapter extends CursorRecyclerViewAdapter<RecyclerView.ViewHolder> {

    private int mBrownColor;
    private int mGreenColor;
    private int mCheckItems;
    private ItemsCheckedCallback mCheckedCallback;

    RecipeDetailCursorAdapter(Context context, Cursor cursor, ItemsCheckedCallback callback) {
        super(context, cursor);
        mCheckedCallback = callback;
        mBrownColor = ContextCompat.getColor(mContext, R.color.colorTextBrown);
        mGreenColor = ContextCompat.getColor(mContext, R.color.colorTextGreen);
        mCheckItems = 0;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, Cursor cursor) {
        String taskName = cursor.getString(cursor.getColumnIndexOrThrow(MascotHelper.RecipeTask.NAME));
        ((RecipeTaskHolder) viewHolder).bindView(taskName);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recipe_task_item, parent, false);
        return new RecipeTaskHolder(v);
    }


    private class RecipeTaskHolder extends RecyclerView.ViewHolder
            implements SmoothCheckBox.OnCheckedChangeListener {

        SmoothCheckBox taskBox;
        TextView recipeComponentName;


        RecipeTaskHolder(View v) {
            super(v);
            taskBox = (SmoothCheckBox) v.findViewById(R.id.recipe_task_box);
            recipeComponentName = (TextView) v.findViewById(R.id.recipe_component_name);
            taskBox.setOnCheckedChangeListener(this);
        }


        void bindView(String task) {
            recipeComponentName.setText(task);
        }

        @Override
        public void onCheckedChanged(SmoothCheckBox smoothCheckBox, boolean b) {
            if (smoothCheckBox.isChecked()) {
                recipeComponentName.setTextColor(mBrownColor);
                mCheckItems++;
            } else {
                recipeComponentName.setTextColor(mGreenColor);
                mCheckItems--;
            }


            if (mCheckItems == getItemCount()) {
                //Toast.makeText(mContext, "All items are checked", Toast.LENGTH_SHORT).show();
                mCheckedCallback.onAllItemChecked(true);
            } else {
                mCheckedCallback.onAllItemChecked(false);
            }
        }
    }


    interface ItemsCheckedCallback {
        void onAllItemChecked(boolean status);
    }
}

任何想法或建议都将不胜感激。

Any ideas or suggestions would be appreciated.

推荐答案

在onBindView上使用以下逻辑:

use logic below on onBindView:

holder.attenCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (holder.attenCheckBox.isChecked())
                    dataModel.setChecked(true);
                else
                    dataModel.setChecked(false);
            }
        });

        if (dataModel.getChecked())
            holder.attenCheckBox.setChecked(true);
        else
            holder.attenCheckBox.setChecked(false);
        holder.checkboxLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (holder.attenCheckBox.isChecked())
                    holder.attenCheckBox.setChecked(false);
                else
                    holder.attenCheckBox.setChecked(true);


            }
        });

说明:


  • 向下或向上滚动时,回收查看会延长eveytime。

  • 您需要在数据pojo中存储标记以跟踪检查状态

  • 将该标志与 setOnCheckedChangeListener 一起使用,可以让你检查启用/禁用。确保你把旗帜放在后面听众。

  • Recycle view inflate eveytime when you scroll down or up.
  • you need to store a flag in the data pojo to keep track of check status
  • using that flag with setOnCheckedChangeListener will enable you to have you checked enable/disable .make sure you put flag after listener.

这篇关于滚动时,在recyclerView中取消选中复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆