当前位置: 首页 > 知识库问答 >
问题:

如何用Android分页Firestore?

林意蕴
2023-03-14
// 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
        // ...
    }
});

怎么办?文档没有太多的细节。

PS:我需要与回收器视图(不是列表视图),当用户滚动。谢谢

共有1个答案

凤修筠
2023-03-14

正如官方文档中提到的,解决这个问题的关键是使用startAfter()方法。因此您可以通过将查询游标与limit()方法组合起来对查询进行分页。您可以使用批处理中的最后一个文档作为下一批处理的光标的开始。

为了解决这个分页问题,请看我在这篇文章中的回答,在这篇文章中,我逐步解释了如何从云Firestore数据库以较小的块加载数据,并在按钮单击时将其显示在ListView中。

解决方案:

让我们首先定义RecyclerView,将布局管理器设置为LinearLayoutManager并创建一个列表。我们还使用空列表实例化适配器,并将适配器设置为recyclerView:

RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
List<ProductModel> list = new ArrayList<>();
ProductAdapter productAdapter = new ProductAdapter(list);
recyclerView.setAdapter(productAdapter);

让我们假设我们有一个如下所示的数据库结构:

Firestore-root
   |
   --- products (collection)
         |
         --- productId (document)
                |
                --- productName: "Product Name"

和一个如下所示的模型类:

public class ProductModel {
    private String productName;

    public ProductModel() {}

    public ProductModel(String productName) {this.productName = productName;}

    public String getProductName() {return productName;}
}
private class ProductAdapter extends RecyclerView.Adapter<ProductViewHolder> {
    private List<ProductModel> list;

    ProductAdapter(List<ProductModel> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product, parent, false);
        return new ProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ProductViewHolder productViewHolder, int position) {
        String productName = list.get(position).getProductName();
        productViewHolder.setProductName(productName);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text_view"
    android:textSize="25sp"/>
private class ProductViewHolder extends RecyclerView.ViewHolder {
    private View view;

    ProductViewHolder(View itemView) {
        super(itemView);
        view = itemView;
    }

    void setProductName(String productName) {
        TextView textView = view.findViewById(R.id.text_view);
        textView.setText(productName);
    }
}
private int limit = 15;

现在让我们定义使用这个限制的查询:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference productsRef = rootRef.collection("products");
Query query = productsRef.orderBy("productName", Query.Direction.ASCENDING).limit(limit);

下面是在您的情况下同样具有魔力的代码

query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (DocumentSnapshot document : task.getResult()) {
                ProductModel productModel = document.toObject(ProductModel.class);
                list.add(productModel);
            }
            productAdapter.notifyDataSetChanged();
            lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);

            RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                        isScrolling = true;
                    }
                }

                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);

                    LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) recyclerView.getLayoutManager());
                    int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
                    int visibleItemCount = linearLayoutManager.getChildCount();
                    int totalItemCount = linearLayoutManager.getItemCount();

                    if (isScrolling && (firstVisibleItemPosition + visibleItemCount == totalItemCount) && !isLastItemReached) {
                        isScrolling = false;
                        Query nextQuery = productsRef.orderBy("productName", Query.Direction.ASCENDING).startAfter(lastVisible).limit(limit);
                        nextQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<QuerySnapshot> t) {
                                if (t.isSuccessful()) {
                                    for (DocumentSnapshot d : t.getResult()) {
                                        ProductModel productModel = d.toObject(ProductModel.class);
                                        list.add(productModel);
                                    }
                                    productAdapter.notifyDataSetChanged();
                                    lastVisible = t.getResult().getDocuments().get(t.getResult().size() - 1);

                                    if (t.getResult().size() < limit) {
                                        isLastItemReached = true;
                                    }
                                }
                            }
                        });
                    }
                }
            };
            recyclerView.addOnScrollListener(onScrollListener);
        }
    }
});

其中LastVisible是一个DocumentSnapshot对象,表示查询中的最后一个可见项。在本例中,每15个变量都声明为全局变量:

private DocumentSnapshot lastVisible;
private boolean isScrolling = false;
private boolean isLastItemReached = false;
    null
 类似资料:
  • 问题内容: 我阅读了Firestore文档以及Internet(stackoverflow)上有关Firestore分页的所有文章,但没有运气。我试图在文档中实现确切的代码,但是什么也没有发生。我有一个包含项目的基本数据库(超过1250个或更多),我想逐步获取它们。通过滚动以加载15个项目(到数据库中的最后一个项目)。 如果使用文档代码: 怎么做?文档没有太多细节。 PS:当用户滚动时,我需要使用

  • 背景: 默认情况下,列出IAM用户的AWS操作返回的最大值为50。 阅读下面的文档(链接),我运行下面的代码,并通过将“MaxItems”设置为1000返回完整的数据集。 http://boto3.readthedocs.io/en/latest/guide/paginators.html https://boto3.readthedocs.io/en/latest/reference/servi

  • 问题内容: 我目前正在开发一个电子商务应用程序,其中必须使用搜索功能显示可用产品的列表。 与每次搜索一样,我必须在此处实施分页。 我使用mybatis作为我的ORM工具,使用mysql作为基础数据库。 谷歌搜索我发现以下方法来完成此任务: 客户端分页 :在这里,我必须一口气从数据库中检索与搜索条件匹配的所有结果,并在我的代码级别(可能是最终代码)处处理分页。 服务器端分页 :使用mysql,我可以

  • 本文向大家介绍Android中ListView如何分页加载数据,包括了Android中ListView如何分页加载数据的使用技巧和注意事项,需要的朋友参考一下 熟悉Android的朋友们都知道,不管是微博客户端还是新闻客户端,都离不开列表组件,可以说列表组件是Android数据展现方面最重要的组件,我们今天就要讲一讲列表组件ListView加载数据的相关内容。通常来说,一个应用在展现大量数据时,不

  • 我希望能够处理从必须在页面中访问的源读取的java流。作为第一种方法,我实现了一个分页迭代器,它在当前页面用尽条目时简单地请求页面,然后使用< code > stream support . stream(iterator,false)获取迭代器上的流句柄。 因为我发现获取我的页面非常昂贵,所以我想通过并行流的方式访问页面。此时,我发现由于java直接从迭代器提供的spliterator实现,我的

  • 我正在使用Struts 2编写网上购物应用程序。 在前端,我使用了jsp、twitter引导、jquery和Moustach。js模板、twbs分页插件和一些javascript。 我有产品实体,我想在jsp页面中向用户显示产品列表。 我这样做的方式是异步加载页面与固定数量(20)的产品在json格式,然后获得他们使用胡子模板。 除了用户第一次看到此jsp页面时,我的所有代码都正常工作—前20个产