I am using a list adapter with recycler view, now the data class contains a variable isChecked, this is used to indicate if the user has selected this variable or not, the code is updating the list as I can see the logs (which I had put for testing purpose) return that the current list is always updated when the user clicks on a item but for some reason the changes in UI (based on isChecked variable) is only reflected when scrolling the recycler view or by clicking other items. I put a notifyDataSetChanged to see if it forces the list to update and see if the updated views are correct and it works, but then this destroys the whole purpose of using a diff util. I have a nested list in my wrapper data class which is being compared in the diff util as illustrated below
private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<MainDataClass>() {
override fun areItemsTheSame(
oldItem: MainDataClass,
newItem: MainDataClass
): Boolean {
if (oldItem is MainDataClass.Item && newItem is MainDataClass.Item) {
return oldItem.data.id == newItem.data.id
} else if (oldItem is MainDataClass.List && newItem is MainDataClass.List) {
return oldItem.list == newItem.list
} else return false
}
override fun areContentsTheSame(
oldItem: MainDataClass,
newItem: MainDataClass
): Boolean {
if (oldItem is MainDataClass.Item && newItem is MainDataClass.Item) {
return oldItem.data == newItem.data
} else if (oldItem is MainDataClass.List && newItem is MainDataClass.List) {
return oldItem.list == newItem.list
} else return false
}
}
MainDataClass.List contains the list of particular items as mentioned above.
public class Item{
private Integer count;
private Integer id;
private String icon_img;
private String name;
private String cover_img;
private String group_name;
private Integer parent_id;
private Integer status;
private boolean checked = false;
private Integer whatToVisible;
public Item(Integer count, Integer id, String icon_img, String cover_img, String group_name, Integer parent_id) {
this.count = count;
this.id = id;
this.icon_img = icon_img;
this.cover_img = cover_img;
this.group_name = group_name;
this.parent_id = parent_id;
this.checked = false;
}
public Item(Integer id, String icon_img, String group_name, Integer parent_id, boolean checked) {
this.id = id;
this.icon_img = icon_img;
this.name = name;
this.group_name = group_name;
this.parent_id = parent_id;
this.checked = checked;
}
public Item(Integer id,Integer parent_id) {
this.id = id;
this.parent_id = parent_id;
}
public static Item objectExample() {
return new TrendingGroupsResponse(-2, -2);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getCount() {
return count;
}
public String getIcon_img() {
return icon_img;
}
public void setIcon_img(String icon_img) {
this.icon_img = icon_img;
}
public String getCover_img() {
return cover_img;
}
@Override
public int hashCode() {
return this.id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Item)) return false;
Item that = (Item) o;
return checked == that.checked && Objects.equals(count, that.count) && Objects.equals(id, that.id) && Objects.equals(icon_img, that.icon_img) && Objects.equals(name, that.name) && Objects.equals(cover_img, that.cover_img) && Objects.equals(group_name, that.group_name) && Objects.equals(parent_id, that.parent_id) && Objects.equals(status, that.status) && Objects.equals(whatToVisible, that.whatToVisible);
}
@Override
public String toString() {
return "id: " + this.id;
}
public void setCover_img(String cover_img) {
this.cover_img = cover_img;
}
public String getGroup_name() {
if (name != null){
group_name = name;
}
return group_name;
}
public void setGroup_name(String group_name) {
this.group_name = group_name;
}
public Integer getParent_id() {
return parent_id;
}
public void setParent_id(Integer parent_id) {
this.parent_id = parent_id;
}
public void setCount(Integer count) {
this.count = count;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getIconImg() {
return icon_img;
}
public void setIconImg(String icon_img) {
this.cover_img = icon_img;
}
public String getCoverImg() {
return cover_img;
}
public void setCoverImg(String cover_img) {
this.cover_img = cover_img;
}
public String getGroupName() {
if (name != null) {
group_name = name;
}
return group_name;
}
public void setGroupName(String group_name) {
this.group_name = group_name;
}
public Integer getWhatToVisible() {
return whatToVisible;
}
public void setWhatToVisible(Integer whatToVisible) {
this.whatToVisible = whatToVisible;
}
Method called when main item is clicked from main list
fun addOrRemoveSelectedItemsIfPresent(item: Item) {
viewModelScope.launch {
addOrRemoveItemsFromPopularItems(item.id.toString())
updateAllItems(item)
var itemList = _selectedItems.value
if (itemList == null) itemList = ArrayList()
itemList.forEach { item1: Item ->
if (item1.id == item.id) {
itemList.remove(group)
selectedItemCount.set(selectedItemCount.get() - 1)
_selectedItems.value = itemList
return@launch
}
}
itemList.add(response)
selectedItemCount.set(selectedItemCount.get() + 1)
_selectedItems.value = itemList
}
}
private fun addOrRemoveItemsFromPopularItems(id: String) {
val popularItems = _popularItemsLiveData.value?.data
popularItems?.forEach {
if (it.id.toString() == id) {
if (it.isChecked == null || it.isChecked == false) {
it.isChecked = true
} else {
it.isChecked = false
}
}
}
_popularItemsLiveData.postValue(Success(popularItems))
}