无法将对象传递到我的自定义复合控件

Not able to pass a object to my custom compound control

我正在为我的 Android 项目使用 MVP。 我有一个 POJO Product,需要能够在单独的 activity 中显示该产品的详细信息。这可以 activity 需要 2 种不同的布局: 1. 带有按钮的布局,用于打印该产品的详细信息 2. 带有 2 个按钮的布局,用于将所选产品添加到心愿单或取消操作

上述两种布局都有相同的 xml 布局来显示细节,所以我想制作一个复合组件,如 desbribed here

我想用 MVP 架构来实现这个。所以我的目标是我有一个 class 扩展 ConstraintLayout 并作为我的观点。该视图有自己的演示者,可以使用 Product.

中的数据设置 TextView s

所有这些都有效,但我无法从具有复合组件的 activity 的演示者那里传递选定的 Product,这导致产品的数据不是化合物中显示。

DetailCompound.java

public class DetailsCompound extends ConstraintLayout {
    TextView productTv, brandTv, sizeTv, extraTv, materialTv, familyTv;
    ImageView codeImg;
    DetailPresenter presenter;

    public DetailsCompound(Context context, Product product) {
        super(context);
        this.initViews(context);
        this.createPresenter(product);
    }

    private void initViews(Context context) {
        inflate(this.getContext(), R.layout.product_details, this);

        //initialize views
    }

    private void createPresenter(Product product) {
        this.presenter = new DetailPresenter(this, product);
    }
}

DetailPresenter.java

public class DetailPresenter implements OnLoadListener {
    DetailsCompound compound;
    StorageManager storage;
    Product product;

    public DetailPresenter(DetailsCompound compound, Product product) {
        this.compound = compound;
        this.storage = new StorageManager();
        this.storage.setOnLoadListener(this);
        this.setProduct(product);
    }

    private void setProduct(Product product) {
        this.product = product;

        //set details of product in view

        this.storage.getProductCodeImage(this.product.getCodeUrl());
    }

    @Override
    public void onLoad(Object loadedObj) {
        this.compound.codeImg.setImageBitmap((Bitmap) loadedObj);
    }
}

包含化合物

activity_product_detail.xml
<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:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:paddingHorizontal="8dp"
    tools:context=".productdetail.ProductDetailActivity">

    <include
        android:id="@+id/product_details"
        layout="@layout/product_details"
        android:layout_width="395dp"
        android:layout_height="534dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/print_btn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/print_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Print"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

ProductDetailActivity.java

public class ProductDetailActivity extends AppCompatActivity {
    Button printBtn;

    DetailsCompound productDetails;
    ProductDetailPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);

        createPresenter();
        initViews();
    }

    private void initViews() {
        this.productDetails = new DetailsCompound(this.getApplicationContext(), this.presenter.product);
        printBtn = (Button)findViewById(R.id.print_btn);
    }

    private void createPresenter() {
        this.presenter = new ProductDetailPresenter(this);
    }
}

我不知道如何 "link" activity_product_detail.xml 中的 <include>DetailCompound.java

您可以通过 Intent

将任何详细信息从您的 POJO 发送到另一个 activity

所以,我想通了...

我的逻辑很好,就是不知道怎么用复合组件。 这是我修复它的方法:

实现复合的activity_product_detail.xml

<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="match_parent"
    android:gravity="center_horizontal"
    android:paddingHorizontal="8dp">

    <com.example.imanager.compounds.details.DetailsCompound
        android:id="@+id/product_details"
        android:layout_width="395dp"
        android:layout_height="534dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/print_btn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/print_btn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Print"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

它没有用,因为我使用了 <include> 标签而不是实现复合 java class 本身。在此 java class 中,充气器用于给可重复使用的 UI 组件充气:

private void initViews() {
        LayoutInflater.from(getContext()).inflate(R.layout.compound_product_details, this);

        //init views
    }

在 activity、ProductDetailActivity.java 中,我们只是实例化化合物,它包含化合物:

this.productDetails = (DetailsCompound)findViewById(R.id.product_details);
        this.productDetails.getPresenter().setProduct(this.presenter.product);