如何删除recycerview gridlayoutmanager中的空白?

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

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


加载gridlayoutmanager的代码:

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

具有以下条件的适配器:

@Override
public void onBindViewHolder(@NonNull final PosProductAdapter.MyViewHolder holder, int position) {
    String pcode = productData.get(position).getProduct_code();
    String productId = productData.get(position).getProductId();
    firebaseFirestore.collection("products")
            .document(pcode)
            .get()
            .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    if (documentSnapshot.exists())
                    {
                        String s = documentSnapshot.getString("pStatus");
                        Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
                        if (s.equals("inactive"))
                        {
                            holder.pos_product_ll.setVisibility(View.GONE);
                            holder.btnAddToCart.setVisibility(View.GONE);
                        }
                    }
                }
            });

xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:background="?android:attr/selectableItemBackground"
    android:layout_width="match_parent"
    android:id="@+id/pos_product_ll"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <androidx.cardview.widget.CardView
            android:id="@+id/card_product"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:foreground="?android:attr/selectableItemBackground"
            android:theme="@style/ThemeOverlay.AppCompat.Light"
            app:cardCornerRadius="10dp"
            app:cardElevation="0dp">
        </androidx.cardview.widget.CardView>
    </LinearLayout>
rsaldnfx

rsaldnfx1#

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

if (s.equals("inactive")){
       holder.pos_product_ll.setVisibility(View.GONE);
       holder.btnAddToCart.setVisibility(View.GONE);
  } else {
       holder.pos_product_ll.setVisibility(View.VISIBLE);
       holder.btnAddToCart.setVisibility(View.VISIBLE);  
  }

相关问题