使用FirestoreRecyclerAdapter(Android)进行Firestore分页

Mr.*_*-on 6 android infinite-scroll firebase android-recyclerview google-cloud-firestore

我在https://firebase.google.com/docs/firestore/query-data/query-cursors上查看了文档

我想与之合作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);
            ....



        }
Run Code Online (Sandbox Code Playgroud)

要实施firestore docs给出的策略,下一步我该如何做。

// 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
        // ...
    }
});
Run Code Online (Sandbox Code Playgroud)

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

我确实查看了有关此主题的其他一些聊天记录,发现的最接近的是https://github.com/Malik333/RecyclerviewFirestorePagination,但这不使用我想使用的FirestoreRecyclerAdapter。

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

(但是关于如何将其与分页连接的信息不多)。

我在想,也许我需要做类似的东西这个

但正在寻找与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.
            }
        });
Run Code Online (Sandbox Code Playgroud)

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

小智 -1

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

\n\n
PagedList.Config config = new PagedList.Config.Builder()\n    .setEnablePlaceholders(false)\n    .setPrefetchDistance(10)\n    .setPageSize(20)\n    .build();\n
Run Code Online (Sandbox Code Playgroud)\n\n

通过使用此代码,您可以定义首次加载时要加载的项目数,以及从数据源一次加载的项目数。\xe2\x80\x99 就是它。\n 您可以从这里了解更多信息

\n\n

FirestoreRecyclerAdapter 和 FirestorePagingAdapter 之间存在细微差别

\n