我当前的领域版本:4.3.3,领域适配器版本: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的方法)。关于android - Realm OrderedRealmCollectionChangeListener不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49048304/