如何删除recycerview gridlayoutmanager中的空白?

lo8azlld  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(402)

我正在制作一个计费应用程序,从firestore加载产品数据。我已将属性设置为“活动-非活动”产品。但当我加载所有产品并隐藏非活动产品时,它显示的是空白。


加载gridlayoutmanager的代码:

  1. GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(), 2);
  2. recyclerView.setLayoutManager(gridLayoutManager); // set LayoutManager to RecyclerView
  3. recyclerView.setHasFixedSize(false);

具有以下条件的适配器:

  1. @Override
  2. public void onBindViewHolder(@NonNull final PosProductAdapter.MyViewHolder holder, int position) {
  3. String pcode = productData.get(position).getProduct_code();
  4. String productId = productData.get(position).getProductId();
  5. firebaseFirestore.collection("products")
  6. .document(pcode)
  7. .get()
  8. .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
  9. @Override
  10. public void onSuccess(DocumentSnapshot documentSnapshot) {
  11. if (documentSnapshot.exists())
  12. {
  13. String s = documentSnapshot.getString("pStatus");
  14. Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
  15. if (s.equals("inactive"))
  16. {
  17. holder.pos_product_ll.setVisibility(View.GONE);
  18. holder.btnAddToCart.setVisibility(View.GONE);
  19. }
  20. }
  21. }
  22. });

xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:orientation="vertical"
  5. android:background="?android:attr/selectableItemBackground"
  6. android:layout_width="match_parent"
  7. android:id="@+id/pos_product_ll"
  8. android:layout_height="wrap_content">
  9. <LinearLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:layout_weight="1"
  13. android:gravity="center"
  14. android:orientation="vertical">
  15. <androidx.cardview.widget.CardView
  16. android:id="@+id/card_product"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:layout_margin="5dp"
  20. android:foreground="?android:attr/selectableItemBackground"
  21. android:theme="@style/ThemeOverlay.AppCompat.Light"
  22. app:cardCornerRadius="10dp"
  23. app:cardElevation="0dp">
  24. </androidx.cardview.widget.CardView>
  25. </LinearLayout>
rsaldnfx

rsaldnfx1#

使用适配器和firebase时,必须正确处理它们。

  1. if (s.equals("inactive")){
  2. holder.pos_product_ll.setVisibility(View.GONE);
  3. holder.btnAddToCart.setVisibility(View.GONE);
  4. } else {
  5. holder.pos_product_ll.setVisibility(View.VISIBLE);
  6. holder.btnAddToCart.setVisibility(View.VISIBLE);
  7. }

相关问题