确定领域 changeSet 字段

Determine realm changeSet fields

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

需要范围信息和更改的实体字段信息。

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

可以参考官方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(因为您希望在视图持有者中处理它本身)。