【问题标题】:Firestore Pagination with FirestoreRecyclerAdapter (Android) [duplicate]Firestore分页与FirestoreRecyclerAdapter(Android)[重复]
【发布时间】:2018-09-15 18:06:40
【问题描述】:

我已经查看了https://firebase.google.com/docs/firestore/query-data/query-cursors 的文档

我想让 Firestore 分页与 FirestoreRecyclerAdapter 一起工作。

有人有这个用例的示例工作代码吗?我有一个列表,该列表可能很长。我想设置一个限制并遵循通过将查询游标与 limit() 方法组合来对查询进行分页的策略。

到目前为止,这就是我的 ListActivity 中的内容:

     // Construct query for first 25 teachers, ordered by firstName
    firstQuery = FirebaseFirestore.getInstance()
            .collection("School").document(user.getUid())
            .collection("teachers")
            .orderBy("firstName")
            .limit(25);


    FirestoreRecyclerOptions<Teacher> options = new FirestoreRecyclerOptions.Builder<Teacher>()
            .setQuery(firstQuery, Teacher.class)
            .build();

    adapter = new FirestoreRecyclerAdapter<Teacher, TeacherHolder>(options) {
        @Override
        public void onBindViewHolder(final TeacherHolder holder, final int position, Teacher teacherItem) {
            // Bind the Teacher object to the TeacherHolder
            //progressBar.setVisibility(View.GONE);
            ....



        }

要实施 firestore 文档给出的策略,下一步我该如何进行。

// Construct query for first 25 cities, ordered by population
Query first = db.collection("cities")
    .orderBy("population")
    .limit(25);

first.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot documentSnapshots) {
        // ...

        // Get the last visible document
        DocumentSnapshot lastVisible = documentSnapshots.getDocuments()
                .get(documentSnapshots.size() -1);

        // Construct a new query starting at this document,
        // get the next 25 cities.
        Query next = db.collection("cities")
                .orderBy("population")
                .startAfter(lastVisible)
                .limit(25);

        // Use the query for pagination
        // ...
    }
});

如何在使用 FirestoreRecyclerAdapter 时将 firestore 给出的上述建议连接到我的应用程序中。我是否将 scrollListener 添加到上面的适配器?并监听滚动事件,重新运行查询。将所有这些联系在一起的示例代码将帮助我清除完成这一切所需的布线。

我确实查看了围绕这个主题的其他一些聊天,我发现最接近的是https://github.com/Malik333/RecyclerviewFirestorePagination,但这并没有使用我想要使用的 FirestoreRecyclerAdapter。

讨论回收器适配器的 Firestore 文档 https://github.com/firebase/FirebaseUI-Android/tree/master/firestore#using-the-firestorerecycleradapter

(但关于如何将其与分页联系起来的信息不多)。

我在想也许我需要做一些类似于this的事情

但正在寻找与 FirestoreRecyclerAdapter 代码的集成。

我正在考虑从

开始
myRecyclerView.setOnScrollChangeListener(new EndlessScrollListener() {
            @Override
            public boolean onLoadMore(int page, int totalItemsCount) {
                // Triggered only when new data needs to be appended to the list
                // Add whatever code is needed to append new items to your AdapterView
                //loadNextDataFromApi(page);
                // or loadNextDataFromApi(totalItemsCount);
                return true; // ONLY if more data is actually being loaded; false otherwise.
            }
        });

并遵循本教程中概述的步骤 https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews-and-RecyclerView

【问题讨论】:

标签: android firebase android-recyclerview google-cloud-firestore infinite-scroll


【解决方案1】:

你可以这样做。这很简单,而不是使用 FirestoreRecyclerAdapter。您只需要使用 FirestorePagingAdapter。它还支持firestoreUi。

PagedList.Config config = new PagedList.Config.Builder()
    .setEnablePlaceholders(false)
    .setPrefetchDistance(10)
    .setPageSize(20)
    .build();

通过使用此代码,您可以定义首次加载时要加载的项目数,以及一次从数据源加载的项目数。而已。 你可以了解更多from here

FirestoreRecyclerAdapter 和 FirestorePagingAdapter 之间存在细微差别

【讨论】:

    猜你喜欢
    • 2018-06-05
    • 2020-05-28
    • 2020-08-30
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    相关资源
    最近更新 更多