0

The setup: (project link)

I use a RecyclerView (with a RealmRecyclerViewAdapter) showing a List of names, which looks like this.

In order to manage the View of the sticky index letter in the top left corner vs the index letter of the item itself, a ScrollListener on the RecyclerView sets the visibility of the sticky index and the item's index via updateStickyIndex().

The problem:

Whenever I delete something in the Realm DB, updating the sticky index in onSuccess() of the RealmTransaction will execute before the items have actually changed on the RecyclerView. In order to call updateStickyIndex() after the items have changed, I have to delay the call in onSuccess().

What am I missing?

kazume
  • 1,333
  • 2
  • 16
  • 30
  • You were missing to notify adapter after remove items from database. – Piyush Patel May 09 '17 at 11:36
  • The RealmRecyclerViewAdapter takes care of that ... so the change is happening, it's just that it happens *after* the onSuccess() callback of the RealmTransaction. – kazume May 09 '17 at 11:39
  • What I'm looking for is if there's a way to know when the adapter itself has finished passing the changes to the recyclerview? – kazume May 09 '17 at 11:42
  • https://github.com/realm/realm-android-adapters/blob/master/adapters/src/main/java/io/realm/RealmRecyclerViewAdapter.java updateData function does notify recyclerview – Piyush Patel May 09 '17 at 12:01
  • updateData is used to change the data set the changelistener is linked to. In my case, I keep using the data set (an OrderedRealmCollection) I passed in the constructor of the adapter. Whenever an Object in that OrderedRealmCollection changes, that change is caught by the listener set to it and the listener's onChange() is called. The problem is, that after the transaction is finished, the adapter hasn't necessarily finished passing those changes to the RecyclerView. I'd like to update the sticky index *after* the recyclerview is updated, but I can't seem to find a hook. – kazume May 09 '17 at 12:15
  • for this thing you need to implement your own method that reflect after delete object from realm. you doesn't need to use RealmRecyclerViewAdapter for such thing implement your own adapter thats work for you to update sticky index. – Piyush Patel May 09 '17 at 12:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/143781/discussion-between-piyush-patel-and-kazume). – Piyush Patel May 09 '17 at 12:26

1 Answers1

0

By overriding onLayoutCompleted() of the LinearLayoutManager (got his from here) the update of the sticky index can be called at the correct moment (after the LayoutManager has actually finished the changes in the DB/Adapter).

Community
  • 1
  • 1
kazume
  • 1,333
  • 2
  • 16
  • 30