0

我正在为 Recycler View 使用 Epoxy Controller。在用户操作更改数据后,我无法更改视图。

基本上,我在视图中有一个切换按钮,该按钮在回收站视图中使用,我正在尝试更新切换按钮状态更改的视图。我在环氧树脂控制器的 setProductList() 函数中调用 requestModelBuild(),但更改未反映在视图中。

public class SellerInventoryListEpoxyController extends EpoxyController {

private List<Product> productList = Collections.emptyList();
private Context context;
private SellerInventoryListEpoxyController.Callbacks callbacks;

public void setProductList(List<Product> productList, Context context, SellerInventoryListEpoxyController.Callbacks callbacks) {
    this.productList = productList;
    this.context = context;
    this.callbacks = callbacks;
    requestModelBuild();
}

@Override
protected void buildModels() {
    for (int i = 0; i < productList.size(); i++) {
        new InventoryProductDetailModel_()
                .id(productList.get(i).getId())
                .product(productList.get(i))
                .position(i)
                .listSize(productList.size())
                .callbacks(callbacks)
                .context(context)
                .addTo(this);
    }
}


public interface Callbacks {
    void onViewComboClick(Product productComboList);

    void onProductListingStatusChanged(Boolean newStatus, int productSellerId);

    void onRecyclerViewReachEnd();
}

}

public class InventoryProductDetailModel extends EpoxyModelWithHolder<InventoryProductDetailModel.ViewHolder> implements CompoundButton.OnCheckedChangeListener {


@EpoxyAttribute
Product product;
@EpoxyAttribute
int position;
@EpoxyAttribute
int listSize;
@EpoxyAttribute
Context context;
@EpoxyAttribute(EpoxyAttribute.Option.DoNotHash)
SellerInventoryListEpoxyController.Callbacks callbacks;

@Override
protected ViewHolder createNewHolder() {
    return new ViewHolder();
}

@Override
protected int getDefaultLayout() {
    return R.layout.inventroy_item_layout;
}

private DrawableCrossFadeFactory factory =
        new DrawableCrossFadeFactory.Builder().setCrossFadeEnabled(true).build();

@Override
public void bind(@NonNull InventoryProductDetailModel.ViewHolder holder) {
    super.bind(holder);
    holder.quantity.setText(String.format("Available :%d", product.getTotalStock()));
    holder.brand.setText(product.getProduct().getBrandName());
    holder.title.setText(product.getProduct().getTitle());
    holder.category.setText(product.getProduct().getCategoryName());
    holder.sku.setText(String.format("Sku: %s", product.getSku()));
    holder.inventoryItemConstrainLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(context, ProductDetailActivity.class);
            intent.putExtra("product_id", product.getId());
            context.startActivity(intent);
        }
    });
    if (product.getProductCombos() != null && product.getProductCombos().size() > 0) {
        holder.variationCount.setVisibility(View.GONE);
        holder.comboBtn.setVisibility(View.VISIBLE);
        holder.comboBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callbacks.onViewComboClick(product);
            }
        });
    }
    if (product.getSellerActive()) {
        holder.productStatusSwitch.setText("Active");
        holder.productStatusSwitch.setOnCheckedChangeListener(null);
        holder.productStatusSwitch.setChecked(true);
        holder.productStatusSwitch.setOnCheckedChangeListener(this);
        holder.productStatusSwitch.setTextColor(context.getResources().getColor(R.color.colorAccent));
    } else {
        holder.productStatusSwitch.setText("Inactive");
        holder.productStatusSwitch.setOnCheckedChangeListener(null);
        holder.productStatusSwitch.setChecked(false);
        holder.productStatusSwitch.setOnCheckedChangeListener(this);
        holder.productStatusSwitch.setTextColor(Color.parseColor("#ff0000"));
    }
    holder.variationCount.setText(format("Variation(%d)", product.getVariantCount()));
    holder.variationCount.setVisibility(View.VISIBLE);
    holder.comboBtn.setVisibility(View.GONE);
    loadImage(holder.productImage, Utils.getRequiredUrlForThisImage(holder.productImage, product.getProduct().getImage()));
    if (position == listSize - 2) {
        callbacks.onRecyclerViewReachEnd();
    }
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    callbacks.onProductListingStatusChanged(isChecked, product.getId());
}

private void loadImage(ImageView imageView, String url) {
    Glide.with(imageView.getContext()).asBitmap()
            .load(Utils.getRequiredUrlForThisImage(imageView, url))
            .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .fitCenter())
            .transition(withCrossFade(factory))
            .placeholder(R.mipmap.product)
            .into(imageView);
}

@Override
public void unbind(@NonNull InventoryProductDetailModel.ViewHolder holder) {
    super.unbind(holder);
}


public static class ViewHolder extends EpoxyHolder {

    TextView quantity, brand, title, category, variationCount, comboBtn;
    ImageView productImage, btn_product_detail;
    ProgressBar progressBar;
    ConstraintLayout inventoryItemConstrainLayout;
    private TextView sku;
    private Switch productStatusSwitch;

    @Override
    protected void bindView(@NonNull View itemView) {
        productStatusSwitch = itemView.findViewById(R.id.productStatusSwitch);
        quantity = itemView.findViewById(R.id.product_qty);
        brand = itemView.findViewById(R.id.product_brand);
        title = itemView.findViewById(R.id.product_title);
        sku = itemView.findViewById(R.id.sku);
        category = itemView.findViewById(R.id.product_category);
        variationCount = itemView.findViewById(R.id.variantCount);
        productImage = itemView.findViewById(R.id.product_image);
        btn_product_detail = itemView.findViewById(R.id.btn_product_detail);
        inventoryItemConstrainLayout = itemView.findViewById(R.id.inventory_item_constrain_layout);
        comboBtn = itemView.findViewById(R.id.combo_btn);
        progressBar = itemView.findViewById(R.id.progressbar);
        progressBar.setVisibility(View.GONE);
    }
}

@Override
public int hashCode() {
    super.hashCode();
    return product.hashCode();
}

@Override
public boolean equals(Object o) {
    return super.equals(o);
}

}

private void addProductListingChangeObserver(final Boolean newStatus, final int productSellerId) {
    ProductUpdate productUpdate = new ProductUpdate();
    productUpdate.setSellerActive(newStatus);
    mInventoryViewModel.updateProductSeller(productSellerId, productUpdate).observe(this, new Observer<Resource<ProductSeller>>() {
        @Override
        public void onChanged(Resource<ProductSeller> productSellerResource) {
            if (productSellerResource.status == Status.ERROR) {
                progressBar.setVisibility(View.GONE);
            } else if (productSellerResource.status == Status.SUCCESS) {
                progressBar.setVisibility(View.GONE);
                if (productSellerResource.data != null && productSellerResource.data.isSellerActive() == newStatus) {
                    for (int i = 0; i < productList.size(); i++) {
                        if (productList.get(i).getId() == productSellerId) {
                            productList.get(i).setSellerActive(newStatus);
                            break;
                        }
                    }
                    sellerInventoryListEpoxyController.setProductList(productList, getContext(), InventoryFragment.this);
                }
            } else {
                progressBar.setVisibility(View.VISIBLE);
            }
        }
    });
}

在 addProductListingChangeObserver() 函数中,productList 的一个对象被修改,新的 productList 被传递给 EpoxyController 并调用 requestModelbuild,但视图没有按预期进行修改。

4

0 回答 0