我想根据列表过滤RealmRecyclerViewAdapter
并获得精细的动画(notifyItemRange*()
)。
我的过滤逻辑:
override fun observeCampaigns(nameQuery: String): Flowable<RealmResults<CampaignRealm>> =
Realm.getDefaultInstance().use { realm ->
realm.where<CampaignRealm>()
.contains(CampaignRealmFields.NAME, nameQuery, Case.INSENSITIVE)
.findAllAsync()
.asFlowable()
.filter { it.isLoaded }
}
问题在于,在适配器内部,changeSet 状态始终为 INITIAL,我假设是因为每次更改查询时我都会创建一个新的 RealmResults 对象。
private OrderedRealmCollectionChangeListener createListener() {
return new OrderedRealmCollectionChangeListener() {
@Override
public void onChange(Object collection, OrderedCollectionChangeSet changeSet) {
if (changeSet.getState() == OrderedCollectionChangeSet.State.INITIAL) {
// IT ALWAYS HITS THIS
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);
}
}
};
}
我在任何地方都找不到解决方案。我不可能是第一个拥有这样一个简单用例的人……对吧?!(我要疯了)
想法?