checkboxes gets unchecked in recyclerView upon scrolling

前端 未结 2 1122
别跟我提以往
别跟我提以往 2020-12-21 14:02

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, w

2条回答
  •  执念已碎
    2020-12-21 14:56

    RecyclerView removes (recycles) the unseen views from the layout on scrolling, this is the basic behavior of recyclerView in order to reduce memory use.

    When a view with a checkbox is recycled, a checked checkbox gets unchecked and if it has a listener, the listener gets called.

    You can remove the listener from the view when it is recycled. Just override the onViewRecycled method.

        @Override
        public void onViewRecycled(@NonNull MyViewHolder holder) {
            holder.checkBox.setOnCheckedChangeListener(null);
            super.onViewRecycled(holder);
        }
    

    When the view is constructed again, while scrolling, your listener will also be added again.

提交回复
热议问题