mercredi 1 mai 2019

Create a FrameLayout view using the Builder Pattern?

I'm creating a view entend FrameLayout now that has about 10 parameters.

I was thinking about using the Builder pattern, similar to how the AlertDialog works. However, I'm not exactly sure what would be the best way to implement this, or if it is even a good idea.

Here is an example of what I was thinking, but with many more variables.

there is problem in this.

I want to use the variable adDetailModel' anddataLoaderin the init thatbuilder` get it.

public class DialogContentList extends FrameLayout {

public static DataLoader dataLoader;
static SwipeRefreshRelativeLayout contentList;
public static AdDetailModel adDetailModel;

public DialogContentList(@NonNull Context context) {
    super(context);

    init(context, null, 0);
}

public DialogContentList(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);

    init(context, attrs, 0);
}

public DialogContentList(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    init(context, attrs, defStyleAttr);
}

public static class Builder {

    private AdDetailModel adDetailModel;
    private DataLoader dataLoader;

    public Builder() {

    }


    public Builder setModel(AdDetailModel adDetailModel) {
        this.adDetailModel = adDetailModel;
        return this;
    }

    public Builder setDataloder(DataLoader dataLoader) {
        this.dataLoader = dataLoader;
        return this;
    }


    public DialogContentList build(final Context context) {
        DialogContentList dialogContentList = new DialogContentList(context);

        return dialogContentList;
    }
}

private void init( Context context, AttributeSet attrs, int defStyleAttr) {

    LayoutInflater.from(context).inflate(R.layout.dialog_content_list, this, true);
    contentList = (SwipeRefreshRelativeLayout) findViewById(R.id.ContentList);
  //
  //        contentList.build(new viewWrapper() {
  //            @Override
 //            public BaseWidget getView() {
 //                return new AdSample(context1, adDetailModel);
//            }
//        }, dataLoader);

 }

}

Aucun commentaire:

Enregistrer un commentaire