vivchar/RendererRecyclerViewAdapter

setItems() always replace current items

HombreTech opened this issue · 1 comments

adapter.setDiffCallback(ItemsDiffCallback())
adapter.setItems(projectsList.data)
class ItemsDiffCallback : DefaultDiffCallback<ViewModel>() {

    override fun areItemsTheSame(oldItem: ViewModel, newItem: ViewModel): Boolean {
        if (oldItem is ProjectsList.Data) {
            return if (newItem is ProjectsList.Data) {
                oldItem.id == newItem.id
            } else {
                false
            }
        } else if (newItem is ProjectsList.Data) {
            return false
        }
        return super.areItemsTheSame(oldItem, newItem)
    }

    override fun areContentsTheSame(oldItem: ViewModel, newItem: ViewModel): Boolean {
        return super.areContentsTheSame(oldItem, newItem)
    }

    override fun getChangePayload(oldItem: ViewModel, newItem: ViewModel): Any? {
        if (oldItem is ProjectsList.Data) {
            if (newItem is ProjectsList.Data) {
                val payload: ArrayList<Long> = ArrayList()
                payload.add(System.currentTimeMillis())
                return payload
            }
        }
        val payload = super.getChangePayload(oldItem, newItem)
        return payload
    }

}

Inside ProjectsList.Data:

override fun equals(obj: Any?): Boolean {
            if (obj is Data) {
                val pm: Data = obj
                return pm.id == this.id
            }
            return false
        }

So, always when i use setItems(projectsList.data) its replace my current list by projectsList.data (items various). Собственно, how to добавить элементы в список не перезаписав текущие?

You should have your local list.

ArrayList<Data> localList = ArrayList<>();

public void update() {
    List<Data> partOfList = getPartOfList();
    localList.addAll(partOfList);
    adapter.setItems(localList)
}