首页 > 解决方案 > 如何在 FirestorePagingAdapter 中获取项目列表?

问题描述

我正在使用 FirestorePagingAdapter 来显示 Firestore 中的项目。加载数据时调用哪个方法?onDataChanged()像 FirestoreRecyclerAdapter在加载数据时会覆盖一个方法。

FirestoreRecyclerAdapter adapter = new FirestoreRecyclerAdapter<Chat, ChatHolder>(options) {
    // ...

    @Override
    public void onDataChanged() {
        // Called each time there is a new query snapshot. You may want to use this method
        // to hide a loading spinner or check for the "no documents" state and update your UI.
        // ...
    }

    @Override
    public void onError(FirebaseFirestoreException e) {
        // Called when there is an error getting a query snapshot. You may want to update
        // your UI to display an error message to the user.
        // ...
    }
};

FirestorePagingAdapter中的等价物onDataChanged()。我已经尝试过onCurrentListChanged(),但currentList的大小始终为零。

@Override
public void onCurrentListChanged(@Nullable PagedList<DocumentSnapshot> currentList) {

                if(currentList.size()==0){
                    Log.e(TAG, "onCurrentListChanged: no data");
                    emptyBoxAnimation.setVisibility(View.VISIBLE);
                }
            }

标签: androidfirebasegoogle-cloud-firestorefirebaseui

解决方案


什么相当于 onDataChanged()FirestorePagingAdapter

是 BaseChangeEventListener 的onDataChanged()方法,它与您在问题中提到的方法同名。从课堂上出现的内容中:

处理特定快照中的所有子事件后触发回调。对于批处理事件很有用,例如在初始加载或大型更新批处理后删除加载指示器。


推荐阅读