我想基于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);
}
}
};
}
我在任何地方都找不到解决方案。我不能成为第一个拥有这样简单用例的人...对吗? (我在这里疯了)
想法?
答案 0 :(得分:0)
您可以执行以下操作: 它的工作对我来说是使用filter获取更新结果并通知您的适配器。 和其他解决方案 请参阅this code
@Override
public Flowable<RealmResults<Campaigns>> searchCampaigns(String nameQuery) {
try (Realm realm = Realm.getDefaultInstance()) {
RealmQuery<Campaigns> query = realm.where(Campaigns.class)
.contains(CampaignRealmFields.NAME, nameQuery, Case.INSENSITIVE)
return query
.findAllAsync()
.asFlowable()
.onBackpressureLatest()
.toObservable()
.flatMap(realmResultsCollectionChange ->
query.findAllAsync().asChangesetObservable());
}
}
searchCampaigns(something).subscribe(new Observer<CollectionChange<RealmResults<Campaigns>>>() {
@Override
public void onSubscribe(Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onNext(CollectionChange<RealmResults<Campaigns>> realmResultsCollectionChange) {
ticketRealmResults=realmResultsCollectionChange.getCollection();
if (realmResultsCollectionChange.getChangeset().getChangeRanges().length > 1) {
OrderedCollectionChangeSet.Range[] changerange = realmResultsCollectionChange.getChangeset().getChangeRanges();
for (int i = changerange.length - 1; i >= 0; i--) {
OrderedCollectionChangeSet.Range range = changerange[i];
notifyItemRangeRemoved(range.startIndex, range.length);
}
}
if (realmResultsCollectionChange.getChangeset().getDeletionRanges().length > 0) {
OrderedCollectionChangeSet.Range[] deletionRanges = realmResultsCollectionChange.getChangeset().getDeletionRanges();
for (int i = deletionRanges.length - 1; i >= 0; i--) {
OrderedCollectionChangeSet.Range range = deletionRanges[i];
notifyItemRangeRemoved(range.startIndex, range.length);
}
}
if (realmResultsCollectionChange.getChangeset().getInsertionRanges().length > 0) {
OrderedCollectionChangeSet.Range[] InsertionRanges = realmResultsCollectionChange.getChangeset().getInsertionRanges();
for (int i = InsertionRanges.length - 1; i >= 0; i--) {
OrderedCollectionChangeSet.Range range = InsertionRanges[i];
notifyItemRangeRemoved(range.startIndex, range.length);
}
}
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});