Android中的Cloud Firestore分页-结果重复

时间:2018-10-29 13:35:19

标签: android android-recyclerview google-cloud-firestore

How to paginate Firestore with Android?
https://www.youtube.com/watch?v=KdgKvLll07s&t=413s
我正在使用上述方法在Android中创建Cloud Firestore分页。 Alex Mamo提供的解决方案。我在使用它时遇到问题,如果我用手指在最后一项的位置上滚动,则有可能重复两次该项目,两次加载两次,有人有解决此问题的想法吗?
我相信在onScrolled方法中,以下是问题的出处:

query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
    if (task.isSuccessful()) {
        List<ProductModel> list = new ArrayList<>();
        for (DocumentSnapshot document : task.getResult()) {
            ProductModel productModel = document.toObject(ProductModel.class);
            list.add(productModel);
        }
        ProductAdapter productAdapter = new ProductAdapter(list);
        recyclerView.setAdapter(productAdapter);
        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);
    }
}});


以下是Alex Mamo的其他相关文件:

产品型号

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();
    }
    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);
        }
    }

}

这些只是代码的一部分,有关完整版本,请访问上面的链接。

0 个答案:

没有答案