@Override public void onChildMoved(DataSnapshot dataSnapshot, String s) { String modelName = dataSnapshot.getKey(); T oldModel = keys.get(modelName); T newModel = dataSnapshot.getValue(itemClass); setKey(modelName, newModel); int index = list.indexOf(oldModel); list.remove(index); if (s == null) { list.add(0, newModel); } else { T previousModel = keys.get(s); int previousIndex = list.indexOf(previousModel); int nextIndex = previousIndex + 1; if (nextIndex == list.size()) { list.add(newModel); } else { list.add(nextIndex, newModel); } } sortList(); adapter.animateTo(list); onChildAfter(dataSnapshot, State.MOVED); }
@Override public void onChildRemoved(DataSnapshot dataSnapshot) { String modelName = dataSnapshot.getKey(); T oldModel = keys.get(modelName); list.remove(oldModel); keys.remove(modelName); sortList(); adapter.animateTo(list); onChildAfter(dataSnapshot, State.REMOVED); }
@Override public void onChildChanged(DataSnapshot dataSnapshot, String s) { String modelName = dataSnapshot.getKey(); T oldModel = keys.get(modelName); T newModel = dataSnapshot.getValue(itemClass); setKey(modelName, newModel); int index = list.indexOf(oldModel); list.set(index, newModel); keys.put(modelName, newModel); sortList(); adapter.animateTo(list); onChildAfter(dataSnapshot, State.CHANGED); }
@Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { T model = dataSnapshot.getValue(itemClass); String modelName = dataSnapshot.getKey(); keys.put(modelName, model); setKey(modelName, model); if (s == null) { list.add(0, model); } else { T previousModel = keys.get(s); int previousIndex = list.indexOf(previousModel); int nextIndex = previousIndex + 1; if (nextIndex == list.size()) { list.add(model); } else { list.add(nextIndex, model); } } sortList(); adapter.animateTo(list); onChildAfter(dataSnapshot, State.ADDED); }
public void setComparator(Comparator<? super T> comparator) { this.comparator = comparator; sortList(); adapter.animateTo(list); }