Facing Issue In Setoncheckedchangelistener Position Value In Recyclerview Android
Solution 1:
You need to create a variable in your data model e.g isChecked or isEnabled. Then when you enable/disable them. Just update your array which you used to populate list. Then onBindViewHoder check the variable value and enable/disable check boxes.
Concept is that rows are recycled once you move up/down. So checkboxes does not retain the values. So you need to retain values in array. And populate them afterwards.
Solution 2:
instead of getting position from onbindviewholder set a tag to your layout which holds position of row, use that tag value when you change onchangelistner
Solution 3:
You can use the notifyItemChanged(int position)
method from the RecyclerView.Adapter class.
From the documentation:
Notify any registered observers that the item at position has changed. Equivalent to calling notifyItemChanged(position, null);.
This is an item change event, not a structural change event. It indicates that any reflection of the data at position is out of date and should be updated. The item at position retains the same identity. As you already have the position, it should work for you.
Solution 4:
You need to maintain a flag as follows in your adapter :
privateboolean onViewHolderBind;
publicViewHolder(View itemView) {
super(itemView);
mCheckBox = (CheckBox) itemView.findViewById(R.id.checkboxId);
mCheckBox.setOnCheckChangeListener(this);
}
@OverridepublicvoidonCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(!onViewHolderBind) {
// perform your process when checkbox changed
notifyDataSetChanged();
}
}
...
@OverridepublicvoidonBindViewHolder(YourAdapter.ViewHolder viewHolder, int position) {
// process other views // ...
onViewHolderBind = true;
viewHolder.mCheckBox.setChecked(trueOrFalse);
onViewHolderBind = false;
}
Solution 5:
I think the problem you are suffering is that the items controlled internally by the adapter are not in sync with your arrays, the problem being that Collections.swap(…)
doesn't do what the adapters perform graphically. A swap()
will exchange the position of two items in an array, but the items inside the range remain in position. However, when a user drags an item from the last position to the first position, all the item positions inside the range do change!
I started using this code myself and fixed it by replacing the swap()
call with the following code (diff):
override fun onItemMove(fromPosition: Int, toPosition: Int): Boolean {
- Collections.swap(items, fromPosition, toPosition)
+ valtemp= items[fromPosition]
+ items.removeAt(fromPosition)
+ items.add(toPosition, temp)
notifyItemMoved(fromPosition, toPosition)
returntrue
}
To illustrate visually the bug, I added a dump()
call which would show the contents of the Adapter after each drag interaction, then I created an adapter with seven elements, conveniently placed in alphabetical order:
Adapter: did load 7
Adapter: Dumping 7 items
Adapter: …0 SharingItem(uri=content://FIRST, kind=Image)
Adapter: …1 SharingItem(uri=content://B, kind=Image)
Adapter: …2 SharingItem(uri=content://C, kind=Image)
Adapter: …3 SharingItem(uri=content://D, kind=Audio)
Adapter: …4 SharingItem(uri=content://E, kind=Audio)
Adapter: …5 SharingItem(uri=content://F, kind=Video)
Adapter: …6 SharingItem(uri=content://LAST, kind=Audio)
Displaying these items in a 2x4 grid, I dragged the last item to the first position. As I dragged the finger up, the item would take place in different parts of the collection during the drag movement, which are highlighted by the dumps for each interaction received by the adapter:
Adapter: Move from 6 to 4
Adapter: Dumping 7 items
Adapter: …0 SharingItem(uri=content://FIRST, kind=Image)
Adapter: …1 SharingItem(uri=content://B, kind=Image)
Adapter: …2 SharingItem(uri=content://C, kind=Image)
Adapter: …3 SharingItem(uri=content://D, kind=Audio)
Adapter: …4 SharingItem(uri=content://LAST, kind=Audio)
Adapter: …5 SharingItem(uri=content://F, kind=Video)
Adapter: …6 SharingItem(uri=content://E, kind=Audio)
Adapter: Move from 4 to 2
Adapter: Dumping 7 items
Adapter: …0 SharingItem(uri=content://FIRST, kind=Image)
Adapter: …1 SharingItem(uri=content://B, kind=Image)
Adapter: …2 SharingItem(uri=content://LAST, kind=Audio)
Adapter: …3 SharingItem(uri=content://D, kind=Audio)
Adapter: …4 SharingItem(uri=content://C, kind=Image)
Adapter: …5 SharingItem(uri=content://F, kind=Video)
Adapter: …6 SharingItem(uri=content://E, kind=Audio)
Adapter: Move from 2 to 0
Adapter: Dumping 7 items
Adapter: …0 SharingItem(uri=content://LAST, kind=Audio)
Adapter: …1 SharingItem(uri=content://B, kind=Image)
Adapter: …2 SharingItem(uri=content://FIRST, kind=Image)
Adapter: …3 SharingItem(uri=content://D, kind=Audio)
Adapter: …4 SharingItem(uri=content://C, kind=Image)
Adapter: …5 SharingItem(uri=content://F, kind=Video)
Adapter: …6 SharingItem(uri=content://E, kind=Audio)
After replacing the swap()
call with the implementation above, the logs showed a much better picture of the internals during interaction:
Adapter: did load 7
Adapter: Dumping 7 items
Adapter: …0 SharingItem(uri=content://FIRST, kind=Image)
Adapter: …1 SharingItem(uri=content://B, kind=Image)
Adapter: …2 SharingItem(uri=content://C, kind=Image)
Adapter: …3 SharingItem(uri=content://D, kind=Audio)
Adapter: …4 SharingItem(uri=content://E, kind=Audio)
Adapter: …5 SharingItem(uri=content://F, kind=Video)
Adapter: …6 SharingItem(uri=content://LAST, kind=Audio)
Adapter: Move from 6 to 4
Adapter: Dumping 7 items
Adapter: …0 SharingItem(uri=content://FIRST, kind=Image)
Adapter: …1 SharingItem(uri=content://B, kind=Image)
Adapter: …2 SharingItem(uri=content://C, kind=Image)
Adapter: …3 SharingItem(uri=content://D, kind=Audio)
Adapter: …4 SharingItem(uri=content://LAST, kind=Audio)
Adapter: …5 SharingItem(uri=content://E, kind=Audio)
Adapter: …6 SharingItem(uri=content://F, kind=Video)
Adapter: Move from 4 to 2
Adapter: Dumping 7 items
Adapter: …0 SharingItem(uri=content://FIRST, kind=Image)
Adapter: …1 SharingItem(uri=content://B, kind=Image)
Adapter: …2 SharingItem(uri=content://LAST, kind=Audio)
Adapter: …3 SharingItem(uri=content://C, kind=Image)
Adapter: …4 SharingItem(uri=content://D, kind=Audio)
Adapter: …5 SharingItem(uri=content://E, kind=Audio)
Adapter: …6 SharingItem(uri=content://F, kind=Video)
Adapter: Move from 2 to 0
Adapter: Dumping 7 items
Adapter: …0 SharingItem(uri=content://LAST, kind=Audio)
Adapter: …1 SharingItem(uri=content://FIRST, kind=Image)
Adapter: …2 SharingItem(uri=content://B, kind=Image)
Adapter: …3 SharingItem(uri=content://C, kind=Image)
Adapter: …4 SharingItem(uri=content://D, kind=Audio)
Adapter: …5 SharingItem(uri=content://E, kind=Audio)
Adapter: …6 SharingItem(uri=content://F, kind=Video)
Post a Comment for "Facing Issue In Setoncheckedchangelistener Position Value In Recyclerview Android"