确定领域changeSet字段

时间:2018-06-05 10:14:44

标签: android kotlin realm realm-mobile-platform realm-list

如何使用基本域适配器侦听器方法确定在RealmObject更新中更改了哪些字段?

需要同时拥有范围信息和更改的实体字段信息。

fun notifyRecyclerViewOfChanges(changeSet: ChangeSet?) { 
   // ?
}

1 个答案:

答案 0 :(得分:2)

您可以参考官方realm-android-adapters了解如何将其整合在一起:

private OrderedRealmCollectionChangeListener createListener() {
    return new OrderedRealmCollectionChangeListener() {
        @Override
        public void onChange(Object collection, OrderedCollectionChangeSet changeSet) {
            if (changeSet.getState() == OrderedCollectionChangeSet.State.INITIAL) { // before Realm 5.0, this is `changeSet == null`
                notifyDataSetChanged();
                return;
            }
            // For deletions, the adapter has to be notified in reverse order.
            OrderedCollectionChangeSet.Range[] deletions = changeSet.getDeletionRanges();
            for (int i = deletions.length - 1; i >= 0; i--) {
                OrderedCollectionChangeSet.Range range = deletions[i];
                notifyItemRangeRemoved(range.startIndex, range.length);
            }

            OrderedCollectionChangeSet.Range[] insertions = changeSet.getInsertionRanges();
            for (OrderedCollectionChangeSet.Range range : insertions) {
                notifyItemRangeInserted(range.startIndex, range.length);
            }

            if (!updateOnModification) {
                return;
            }

            OrderedCollectionChangeSet.Range[] modifications = changeSet.getChangeRanges();
            for (OrderedCollectionChangeSet.Range range : modifications) {
                notifyItemRangeChanged(range.startIndex, range.length);
            }
        }
    };
}

如果需要字段级更改,则RealmObject需要使用RealmObjectChangeListener拥有自己的更改侦听器,而updateOnModification应该为false(因为您希望在视图持有者本身中处理它)