Notify Observer when item is added to List of LiveData

后端 未结 8 1195
一个人的身影
一个人的身影 2020-12-07 20:21

I need to get an Observer event when the item is added to the List of LiveData. But as far as I understand the event receives only when I replace the old list with a new one

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-07 20:22

    I ran into the same problem and decided that adding "value = value" or a call to another method everywhere was too much of a pain and very error-prone.

    So, I created my own classes that wrap the collections. Observe will now observe the contents of the collection instead of the collection itself.

    The code is available on GitHub at: ObservableCollections

    This is a FIRST version so please excuse any problems. It is not yet available for gradle include so you will have to download it and add it as a library module to you application.

    At present it includes the following collections:

    ArrayDeque
    ArrayList
    LinkedList
    Queue
    Stack
    Vector
    

    More will be added as time allows. Feel free to request specific ones.

    And PLEASE report any problems.

    Edit: It now includes far more collections. See there for details.
    Also, it is available for include via gradle. Again, see there for details.

提交回复
热议问题