更新回收器视图时的UI错误

发布于 2025-01-24 06:02:09 字数 13934 浏览 0 评论 0原文

我正在使用firbaseui firestorePagingAdapter进行回收器视图适配器,而回收器视图使用带有2列的gridlayout类型。回收器视图项目块cantains 1图像视图和其他很少的文本视图,当我向下滚动列表并向上滚动返回图像视图大小降低时,每次我向下滚动并返回时,此错误都会继续。 当我更新适配器时,也会发生这种情况。

以下是它的代码。顺便说一句,我正在使用导航组件UI。

private FirestorePagingAdapter<ProductModel, HomePagingHolder> pagingAdapter; 
private FirestorePagingOptions<ProductModel> pagingOptions;
private final PagingConfig config = new PagingConfig(15, 10, false);

POJO类

public class ProductModel {
    private boolean availability;
    private DocumentReference category_reference;
    private boolean customizable;
    private String delivery_fee;
    private String description;
    private String max_order_quantity;
    private String name;
    private String price;
    private List<String> thumbnails;
    private String productID;

    public ProductModel(){}

    public ProductModel(boolean availability, DocumentReference category_reference, boolean customizable,
                        String delivery_fee, String description, String max_order_quantity, String name, String price, List<String> thumbnails) {
        this.availability = availability;
        this.category_reference = category_reference;
        this.customizable = customizable;
        this.delivery_fee = delivery_fee;
        this.description = description;
        this.max_order_quantity = max_order_quantity;
        this.name = name;
        this.price = price;
        this.thumbnails = thumbnails;
    }

    public boolean isAvailability() {
        return availability;
    }

    public void setAvailability(boolean availability) {
        this.availability = availability;
    }

    public DocumentReference getCategory_reference() {
        return category_reference;
    }

    public void setCategory_reference(DocumentReference category_reference) {
        this.category_reference = category_reference;
    }

    public String getProductID() {
        return productID;
    }

    public void setProductID(String productID) {
        this.productID = productID;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public boolean isCustomizable() {
        return customizable;
    }

    public void setCustomizable(boolean customizable) {
        this.customizable = customizable;
    }

    public String getDelivery_fee() {
        return delivery_fee;
    }

    public void setDelivery_fee(String delivery_fee) {
        this.delivery_fee = delivery_fee;
    }

    public String getMax_order_quantity() {
        return max_order_quantity;
    }

    public void setMax_order_quantity(String max_order_quantity) {
        this.max_order_quantity = max_order_quantity;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public  List<String> getThumbnails() {
        return thumbnails;
    }

    public void setThumbnails( List<String> thumbnails) {
        this.thumbnails = thumbnails;
    }
}

视图持有人类

static class HomePagingHolder extends RecyclerView.ViewHolder {
        private final TextView pName;
        private final TextView pPrice;
        private final ImageView pImg;
        private final TextView pThumbnailCount;

        public HomePagingHolder(@NonNull View itemView) {
            super(itemView);
            pName = itemView.findViewById(R.id.productName);
            pPrice = itemView.findViewById(R.id.productPrice);
            pImg = itemView.findViewById(R.id.productImg);
            pThumbnailCount = itemView.findViewById(R.id.thumbnail_count_txt);
        }
    }

片段中的

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    ...
    ...
    if (pagingOptions == null) {
            getProductFromFireStore(false);
            initFirestoreUIAdapter();
        }
}

获取数据

private void getProductFromFireStore(boolean check) {
        pagingOptions = new FirestorePagingOptions.Builder<ProductModel>().setLifecycleOwner(this).setQuery(productCollection,
                config, snapshot -> {
                    ProductModel model = snapshot.toObject(ProductModel.class);
                    if (model != null) {
                        model.setProductID(snapshot.getId());
                    }
                    return Objects.requireNonNull(model);
                }).build();
        if (check) {
            pagingAdapter.updateOptions(pagingOptions);
        }
    }

productCollection setquery(...)内部指向Firestore内部的集合参考。

适配器

private void initFirestoreUIAdapter() {
        pagingAdapter = new FirestorePagingAdapter<ProductModel, HomePagingHolder>(pagingOptions) {
            @Override
            protected void onBindViewHolder(@NonNull HomePagingHolder holder, int position, @NonNull ProductModel model) {
                holder.pName.setText(model.getName());
                holder.pPrice.setText(String.format("%s %s", getResources().getString(R.string.rupee_symbol), model.getPrice()));
                holder.pThumbnailCount.setText(String.valueOf(model.getThumbnails().size()));
                Glide.with(requireContext()).load(model.getThumbnails().get(0)).into(holder.pImg);

                holder.itemView.setOnClickListener(view -> {
                    bundle.putString(Fire.FIELD_PRODUCT_ID, model.getProductID());
                    NavHostFragment.findNavController(HomeFragment.this).navigate(R.id.action_home_to_productFragment2, bundle);
                });

            }

            @NonNull
            @Override
            public HomePagingHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(requireContext()).inflate(R.layout.block_home_product_item, parent, false);
                return new HomePagingHolder(v);
            }
        };
    }

回收器查看项目块

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/product_item_ripple"
    android:clickable="true"
    android:focusable="true">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/imgCard"
        android:layout_width="180dp"
        android:layout_height="220dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        app:cardBackgroundColor="@color/colorSurface"
        app:cardCornerRadius="8dp"
        app:cardElevation="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical">

            <androidx.cardview.widget.CardView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                app:cardCornerRadius="8dp"
                app:cardElevation="0dp">

                <ImageView
                    android:id="@+id/productImg"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:background="@color/colorSurface"
                    android:src="@drawable/log_in_lounge_bg"
                    tools:ignore="ContentDescription" />

            </androidx.cardview.widget.CardView>

            <TextView
                android:id="@+id/thumbnail_count_txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentBottom="true"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:background="@drawable/white_txt_bg"
                android:drawablePadding="4dp"
                android:elevation="4dp"
                android:fontFamily="@font/nunito"
                android:text="3"
                app:drawableEndCompat="@drawable/ic_multiple_image" />

        </RelativeLayout>

    </com.google.android.material.card.MaterialCardView>

    <TextView
        android:id="@+id/productName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:ellipsize="end"
        android:fontFamily="@font/montserrat"
        android:maxLines="2"
        android:text="Arm chair blue color"
        android:textSize="16sp"
        app:layout_constraintEnd_toEndOf="@+id/imgCard"
        app:layout_constraintStart_toStartOf="@+id/imgCard"
        app:layout_constraintTop_toBottomOf="@+id/imgCard" />

    <TextView
        android:id="@+id/productPrice"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:fontFamily="@font/nunito"
        android:text="TextView"
        android:textColor="@color/colorTextMini"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/productName"
        app:layout_constraintStart_toStartOf="@+id/productName"
        app:layout_constraintTop_toBottomOf="@+id/productName" />

</androidx.constraintlayout.widget.ConstraintLayout>

项目块

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/product_item_ripple"
    android:clickable="true"
    android:focusable="true">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/imgCard"
        android:layout_width="180dp"
        android:layout_height="220dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        app:cardBackgroundColor="@color/colorSurface"
        app:cardCornerRadius="8dp"
        app:cardElevation="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical">

            <com.google.android.material.imageview.ShapeableImageView
                android:id="@+id/productImg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:layout_centerInParent="true"
                android:background="@color/colorSurface"
                android:src="@drawable/log_in_lounge_bg"
                app:shapeAppearance="@style/IsthmusShapeableImageStyle"
                tools:ignore="ContentDescription" />

            <TextView
                android:id="@+id/thumbnail_count_txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentBottom="true"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:background="@drawable/white_txt_bg"
                android:drawablePadding="4dp"
                android:elevation="4dp"
                android:fontFamily="@font/nunito"
                tools:text="3"
                app:drawableEndCompat="@drawable/ic_multiple_image" />

        </RelativeLayout>

    </com.google.android.material.card.MaterialCardView>

    <TextView
        android:id="@+id/productName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:ellipsize="end"
        android:fontFamily="@font/montserrat"
        android:maxLines="2"
        tools:text="Arm chair blue color"
        android:textSize="16sp"
        app:layout_constraintEnd_toEndOf="@+id/imgCard"
        app:layout_constraintStart_toStartOf="@+id/imgCard"
        app:layout_constraintTop_toBottomOf="@+id/imgCard" />

    <TextView
        android:id="@+id/productPrice"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:fontFamily="@font/nunito"
        tools:text="TextView"
        android:textColor="@color/colorTextMini"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/productName"
        app:layout_constraintStart_toStartOf="@+id/productName"
        app:layout_constraintTop_toBottomOf="@+id/productName" />

</androidx.constraintlayout.widget.ConstraintLayout>

I'm using FirebaseUI FirestorePagingAdapter for recycler view adapter and the recycler view uses GridLayout type with 2 columns . The recycler view item block cantains 1 image view and few others textView , when I scroll down the list and scroll back up top the image view size decreases , this bug continues every time I scroll down and back up .
It also happens when I update the adapter .

Below is the code for it . Btw I'm using Navigation Component UI.

private FirestorePagingAdapter<ProductModel, HomePagingHolder> pagingAdapter; 
private FirestorePagingOptions<ProductModel> pagingOptions;
private final PagingConfig config = new PagingConfig(15, 10, false);

POJO class

public class ProductModel {
    private boolean availability;
    private DocumentReference category_reference;
    private boolean customizable;
    private String delivery_fee;
    private String description;
    private String max_order_quantity;
    private String name;
    private String price;
    private List<String> thumbnails;
    private String productID;

    public ProductModel(){}

    public ProductModel(boolean availability, DocumentReference category_reference, boolean customizable,
                        String delivery_fee, String description, String max_order_quantity, String name, String price, List<String> thumbnails) {
        this.availability = availability;
        this.category_reference = category_reference;
        this.customizable = customizable;
        this.delivery_fee = delivery_fee;
        this.description = description;
        this.max_order_quantity = max_order_quantity;
        this.name = name;
        this.price = price;
        this.thumbnails = thumbnails;
    }

    public boolean isAvailability() {
        return availability;
    }

    public void setAvailability(boolean availability) {
        this.availability = availability;
    }

    public DocumentReference getCategory_reference() {
        return category_reference;
    }

    public void setCategory_reference(DocumentReference category_reference) {
        this.category_reference = category_reference;
    }

    public String getProductID() {
        return productID;
    }

    public void setProductID(String productID) {
        this.productID = productID;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public boolean isCustomizable() {
        return customizable;
    }

    public void setCustomizable(boolean customizable) {
        this.customizable = customizable;
    }

    public String getDelivery_fee() {
        return delivery_fee;
    }

    public void setDelivery_fee(String delivery_fee) {
        this.delivery_fee = delivery_fee;
    }

    public String getMax_order_quantity() {
        return max_order_quantity;
    }

    public void setMax_order_quantity(String max_order_quantity) {
        this.max_order_quantity = max_order_quantity;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public  List<String> getThumbnails() {
        return thumbnails;
    }

    public void setThumbnails( List<String> thumbnails) {
        this.thumbnails = thumbnails;
    }
}

ViewHolder class

static class HomePagingHolder extends RecyclerView.ViewHolder {
        private final TextView pName;
        private final TextView pPrice;
        private final ImageView pImg;
        private final TextView pThumbnailCount;

        public HomePagingHolder(@NonNull View itemView) {
            super(itemView);
            pName = itemView.findViewById(R.id.productName);
            pPrice = itemView.findViewById(R.id.productPrice);
            pImg = itemView.findViewById(R.id.productImg);
            pThumbnailCount = itemView.findViewById(R.id.thumbnail_count_txt);
        }
    }

Inside the Fragment

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    ...
    ...
    if (pagingOptions == null) {
            getProductFromFireStore(false);
            initFirestoreUIAdapter();
        }
}

Getting the data

private void getProductFromFireStore(boolean check) {
        pagingOptions = new FirestorePagingOptions.Builder<ProductModel>().setLifecycleOwner(this).setQuery(productCollection,
                config, snapshot -> {
                    ProductModel model = snapshot.toObject(ProductModel.class);
                    if (model != null) {
                        model.setProductID(snapshot.getId());
                    }
                    return Objects.requireNonNull(model);
                }).build();
        if (check) {
            pagingAdapter.updateOptions(pagingOptions);
        }
    }

productCollection inside the setQuery(...) points to a Collection reference inside Firestore.

Adapter

private void initFirestoreUIAdapter() {
        pagingAdapter = new FirestorePagingAdapter<ProductModel, HomePagingHolder>(pagingOptions) {
            @Override
            protected void onBindViewHolder(@NonNull HomePagingHolder holder, int position, @NonNull ProductModel model) {
                holder.pName.setText(model.getName());
                holder.pPrice.setText(String.format("%s %s", getResources().getString(R.string.rupee_symbol), model.getPrice()));
                holder.pThumbnailCount.setText(String.valueOf(model.getThumbnails().size()));
                Glide.with(requireContext()).load(model.getThumbnails().get(0)).into(holder.pImg);

                holder.itemView.setOnClickListener(view -> {
                    bundle.putString(Fire.FIELD_PRODUCT_ID, model.getProductID());
                    NavHostFragment.findNavController(HomeFragment.this).navigate(R.id.action_home_to_productFragment2, bundle);
                });

            }

            @NonNull
            @Override
            public HomePagingHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(requireContext()).inflate(R.layout.block_home_product_item, parent, false);
                return new HomePagingHolder(v);
            }
        };
    }

Recycler view item block

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/product_item_ripple"
    android:clickable="true"
    android:focusable="true">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/imgCard"
        android:layout_width="180dp"
        android:layout_height="220dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        app:cardBackgroundColor="@color/colorSurface"
        app:cardCornerRadius="8dp"
        app:cardElevation="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical">

            <androidx.cardview.widget.CardView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                app:cardCornerRadius="8dp"
                app:cardElevation="0dp">

                <ImageView
                    android:id="@+id/productImg"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:background="@color/colorSurface"
                    android:src="@drawable/log_in_lounge_bg"
                    tools:ignore="ContentDescription" />

            </androidx.cardview.widget.CardView>

            <TextView
                android:id="@+id/thumbnail_count_txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentBottom="true"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:background="@drawable/white_txt_bg"
                android:drawablePadding="4dp"
                android:elevation="4dp"
                android:fontFamily="@font/nunito"
                android:text="3"
                app:drawableEndCompat="@drawable/ic_multiple_image" />

        </RelativeLayout>

    </com.google.android.material.card.MaterialCardView>

    <TextView
        android:id="@+id/productName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:ellipsize="end"
        android:fontFamily="@font/montserrat"
        android:maxLines="2"
        android:text="Arm chair blue color"
        android:textSize="16sp"
        app:layout_constraintEnd_toEndOf="@+id/imgCard"
        app:layout_constraintStart_toStartOf="@+id/imgCard"
        app:layout_constraintTop_toBottomOf="@+id/imgCard" />

    <TextView
        android:id="@+id/productPrice"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:fontFamily="@font/nunito"
        android:text="TextView"
        android:textColor="@color/colorTextMini"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/productName"
        app:layout_constraintStart_toStartOf="@+id/productName"
        app:layout_constraintTop_toBottomOf="@+id/productName" />

</androidx.constraintlayout.widget.ConstraintLayout>

Item block

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/product_item_ripple"
    android:clickable="true"
    android:focusable="true">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/imgCard"
        android:layout_width="180dp"
        android:layout_height="220dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        app:cardBackgroundColor="@color/colorSurface"
        app:cardCornerRadius="8dp"
        app:cardElevation="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical">

            <com.google.android.material.imageview.ShapeableImageView
                android:id="@+id/productImg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:layout_centerInParent="true"
                android:background="@color/colorSurface"
                android:src="@drawable/log_in_lounge_bg"
                app:shapeAppearance="@style/IsthmusShapeableImageStyle"
                tools:ignore="ContentDescription" />

            <TextView
                android:id="@+id/thumbnail_count_txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentBottom="true"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:background="@drawable/white_txt_bg"
                android:drawablePadding="4dp"
                android:elevation="4dp"
                android:fontFamily="@font/nunito"
                tools:text="3"
                app:drawableEndCompat="@drawable/ic_multiple_image" />

        </RelativeLayout>

    </com.google.android.material.card.MaterialCardView>

    <TextView
        android:id="@+id/productName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:ellipsize="end"
        android:fontFamily="@font/montserrat"
        android:maxLines="2"
        tools:text="Arm chair blue color"
        android:textSize="16sp"
        app:layout_constraintEnd_toEndOf="@+id/imgCard"
        app:layout_constraintStart_toStartOf="@+id/imgCard"
        app:layout_constraintTop_toBottomOf="@+id/imgCard" />

    <TextView
        android:id="@+id/productPrice"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:fontFamily="@font/nunito"
        tools:text="TextView"
        android:textColor="@color/colorTextMini"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/productName"
        app:layout_constraintStart_toStartOf="@+id/productName"
        app:layout_constraintTop_toBottomOf="@+id/productName" />

</androidx.constraintlayout.widget.ConstraintLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文