我正在使用 Android 架构组件(Model-View-ViewModel)geoQuery.addGeoQueryDataEventListener()
从GeoFirestore获取结果
实施 'com.github.imperiumlabs:GeoFirestore-Android:v1.5.0'
每个事件都有自己的回调,我对它们都感兴趣。
我也在使用BottomNavigationView
我只有一个活动并且我所有的逻辑都放在片段中的意思。
我从实现onDocumentEntered()
如下所示开始,我意识到当我导航到下一个活动并返回到调用MVVM的上一个活动时,recyclerView 会立即复制数据。
public class FirestoreGeoQuery extends LiveData<List<StoreModel>> {
private static final String TAG = "debinf FBGeoQuery";
private GeoQuery geoQuery;
private Class clazz;
private List<StoreModel> itemList = new ArrayList<>();
public FirestoreGeoQuery(GeoQuery geoQuery, Class clazz) {
this.geoQuery = geoQuery;
this.clazz = clazz;
}
GeoQueryDataEventListener geoQueryDataEventListener = new GeoQueryDataEventListener() {
@Override
public void onDocumentEntered(@NotNull DocumentSnapshot documentSnapshot, @NotNull GeoPoint geoPoint) {
Log.i(TAG, "onDocumentEntered: "+documentSnapshot);
if (documentSnapshot.exists()) {
StoreModel item = (StoreModel) documentSnapshot.toObject(clazz);
item.setGeoPoint(geoPoint);
Log.i(TAG, "onDocumentEntered: addGeoQueryDataEventListener - store.name: "+item.getName()+", address: "+item.getAddress()+", geoPoint: "+item.getGeoPoint());
itemList.add(item);
}
}
@Override
public void onDocumentExited(@NotNull DocumentSnapshot documentSnapshot) {
Log.i(TAG, "onDocumentExited: ");
}
@Override
public void onDocumentMoved(@NotNull DocumentSnapshot documentSnapshot, @NotNull GeoPoint geoPoint) {
Log.i(TAG, "onDocumentMoved: ");
}
@Override
public void onDocumentChanged(@NotNull DocumentSnapshot documentSnapshot, @NotNull GeoPoint geoPoint) {
Log.i(TAG, "onDocumentChanged: ");
}
@Override
public void onGeoQueryReady() {
Log.i(TAG, "onGeoQueryReady: ");
setValue(itemList);
}
@Override
public void onGeoQueryError(@NotNull Exception e) {
Log.i(TAG, "onGeoQueryError: ");
}
};
@Override
protected void onActive() {
super.onActive();
geoQuery.addGeoQueryDataEventListener(geoQueryDataEventListener);
}
@Override
protected void onInactive() {
super.onInactive();
if (!hasActiveObservers()) {
geoQuery.removeGeoQueryEventListener(geoQueryDataEventListener);
}
}
}
所以我的问题是:如何正确处理每个事件回调的结果?
我很感激任何帮助!