Realm OrderedRealmCollectionChangeListener不起作用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Realm OrderedRealmCollectionChangeListener不起作用相关的知识,希望对你有一定的参考价值。
我目前的境界版本:4.3.3,realm-adapter版本:2.1.1(最新版本)
我使用OrderedRealmCollectionChangeListener作为Realm文档:https://realm.io/docs/java/latest/#notifications
但是,只有第一个元素添加到适配器才会刷新OrderedRealmCollectionChangeListener,直到适配器刷新为止。我无法找到我犯错误的地方。
请帮我。
MyAdapter:
public class MyAdapter<V extends View & BindableItem>
extends MyRealmRecyclerViewAdapter<Model, RecyclerView.ViewHolder> {
private final
@NonNull
LayoutInflater inflater;
public MyAdapter(@Nullable ItemClickListener clickListener,
OrderedRealmCollection<Model> data) {
super(data);
this.inflater = LayoutInflater.from(context);
this.realm = MyApplication.getInstance().openRealm(this.getClass().getName());
}
public boolean realmControl() {
if (realm == null || realm.isClosed()) {
realm = MyApplication.getInstance().openRealm(this.getClass().getName());
updateData(new ModelQueries().getAllModels(getRealm()));
notifyView();
return true;
} else {
return false;
}
}
MyRealmRecyclerViewAdapter:
abstract class MyRealmRecyclerViewAdapter<T extends Model, VH extends RecyclerView.ViewHolder> extends RealmRecyclerViewAdapter<Model, VH> {
protected Context context;
private String TAG = MyRealmRecyclerViewAdapter.class.getSimpleName();
protected Realm realm;
private OrderedRealmCollection<T> allModels;
private int allModelsCount = 0;
private final OrderedRealmCollectionChangeListener listener;
MyRealmRecyclerViewAdapter(@Nullable OrderedRealmCollection<T> data) {
super((OrderedRealmCollection<Model>) data, true, true);
if (data != null && !data.isManaged())
throw new IllegalStateException("Only use this adapter with managed RealmCollection, " +
"for un-managed lists you can just use the BaseRecyclerViewAdapter");
this.context = MyApplication.getContext();
this.allModels = data;
this.listener = createListener();
}
private OrderedRealmCollectionChangeListener createListener() {
return new OrderedRealmCollectionChangeListener() {
@Override
public void onChange(@NonNull Object collection, OrderedCollectionChangeSet changeSet) {
Mylog.i(TAG, " changeSet : " + changeSet);
if (changeSet == null) {
return;
}
OrderedCollectionChangeSet.Range[] insertions = changeSet.getInsertionRanges();
if (insertions.length > 0) {
newModelsControl(getItemCount() - allModelsCount, getItemCount() - 1);
refreshAllModelsCount();
}
}
};
}
@Override
public int getItemCount() {
try {
if (getData() == null) {
return 0;
} else {
return getData().size();
}
} catch (Exception e) {
restartActivity();
return 0;
}
}
public void refreshAllModelsCount() {
allModelsCount = getItemCount();
}
public Realm getRealm() {
if (realm == null || realm.isClosed()) {
realm = MyApplication.getInstance().openRealm(this.getClass().getName());
}
return realm;
}
private boolean isDataValid() {
return getData() != null && getData().size() > 0 && getData().isValid();
}
@Override
public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
if (isDataValid()) {
//noinspection ConstantConditions
addListener(allModels);
}
}
@Override
public void onDetachedFromRecyclerView(final RecyclerView recyclerView) {
super.onDetachedFromRecyclerView(recyclerView);
if (isDataValid()) {
//noinspection ConstantConditions
removeListener(allModels);
}
}
private void addListener(@NonNull OrderedRealmCollection<T> data) {
if (data instanceof RealmResults) {
RealmResults<T> results = (RealmResults<T>) data;
//noinspection unchecked
results.addChangeListener(listener);
} else if (data instanceof RealmList) {
RealmList<T> list = (RealmList<T>) data;
//noinspection unchecked
list.addChangeListener(listener);
} else {
throw new IllegalArgumentException("RealmCollection not supported: " + data.getClass());
}
}
private void removeListener(@NonNull OrderedRealmCollection<T> data) {
if (data instanceof RealmResults) {
RealmResults<T> results = (RealmResults<T>) data;
//noinspection unchecked
results.removeChangeListener(listener);
} else if (data instanceof RealmList) {
RealmList<T> list = (RealmList<T>) data;
//noinspection unchecked
list.removeChangeListener(listener);
} else {
throw new IllegalArgumentException("RealmCollection not supported: " + data.getClass());
}
}
/**
* Returns the item associated with the specified position.
* Can return {@code null} if provided Realm instance by {@link OrderedRealmCollection} is closed.
*
* @param index index of the item.
* @return the item at the specified position, {@code null} if adapter data is not valid.
*/
@SuppressWarnings("WeakerAccess")
public T getItem(int index) {
try {
return isDataValid() ? allModels.get(index) : null;
} catch (Exception e) {
Mylog.printStackTrace(" MyRealmRecyclerViewAdapter getItem error ", e);
return null;
}
}
}
答案
用这个
abstract class MyAdapter<T extends Model, VH extends RecyclerView.ViewHolder> extends RealmRecyclerViewAdapter<Model, VH> {
protected Context context;
private String TAG = "MyAdapter";
protected Realm realm;
MyAdapter(@Nullable OrderedRealmCollection<T> data, Realm realm) {
super((OrderedRealmCollection<Model>) data, true);
if (data != null && !data.isManaged())
throw new IllegalStateException("Only use this adapter with managed RealmCollection, " +
"for un-managed lists you can just use the BaseRecyclerViewAdapter");
this.context = MyApp.getContext();
this.realm = realm;
}
}
你有一堆RealmRecyclerViewAdapter
已经完成的逻辑。
用getAllModels()
替换getData()
,用setAllModels()
替换updateData()
(RealmRecyclerViewAdapter的方法)。
以上是关于Realm OrderedRealmCollectionChangeListener不起作用的主要内容,如果未能解决你的问题,请参考以下文章