Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EpoxyModelGroup with Android Data Binding #327

Closed
BrandonJF opened this issue Oct 19, 2017 · 7 comments
Closed

EpoxyModelGroup with Android Data Binding #327

BrandonJF opened this issue Oct 19, 2017 · 7 comments

Comments

@BrandonJF
Copy link

Epoxy now supports models creation from data binding layouts, but there doesn't seem to be an accessible way of defining a data bound modelgroup/container and then inserting other data bound models within it.

For my use case, I have a controller that contains many dynamically created Carousels. I need to be able to group some of those carousels into the same section with a shared background and header as defined in the section container's created viewmodel. Hopefully, this is clear enough, but any assistance would be appreciated.

@elihart
Copy link
Contributor

elihart commented Oct 20, 2017

You should be able to use models created via databinding in a model group without issue.

You can't create an EpoxyModelGroup via databinding though. The model group should just be a thin shell that organizes all of the views inside it, it shouldn't need to do much logic itself

@BrandonJF
Copy link
Author

BrandonJF commented Oct 20, 2017

Thank you so much for your reply, it's great to know that you're committed to the project and I'd love to help once I have a better understanding. So, for the view here that contains the North End header with the description and the carousel within the same partition, if we wanted to dynamically set the background color for that whole section, would that be defined within the modelgroup?
image

@elihart
Copy link
Contributor

elihart commented Oct 21, 2017

Yes, if you wanted to group the header and carousel I would recommend making the header one model (with title, button, and description), and the carousel a separate model.

The group would be very simple to hold them

public class GroupExample extends EpoxyModelGroup {
  @EpoxyAttribute int backgroundColor;

  public GroupExample(EpoxyModel<?>... models) {
    super(R.layout.group_layout, models);
  }

  @Override
  public void bind(Holder holder) {
    super.bind(holder);
    holder.getRootView().setBackgroundColor(backgroundColor);
  }
  
}

with a layout like

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#334324"/>

and you could use the group in your controller like

new GroupExample_(headerModel, carouselModel)
        .backgroundColor(Color.RED)
        .addTo(controller);

So all the group is doing is adding the models in order in a vertical linear layout, and changing the background color of the linear layout.

The model group is not super sophisticated, it doesn't recycle inner views between other groups. See #289 for more info. so you have to be careful to use a different layout file for different types of models.

If this is a common use case for you a simpler way might be to make backgroundColor a property of each of your models and set the color directly on each model. I try to do as much customization as possible in each individual model and only do grouping when absolutely necessary.

Let me know if that clears it up or you still have questions.

I would love to support recycling of nested views, like that linked issues talks about. That would make model groups much better all around. If you ever want to contribute you'd be most welcome :)

@elihart
Copy link
Contributor

elihart commented Oct 21, 2017

btw I realized that generating a model group subclass like that will require this change #330

@elihart
Copy link
Contributor

elihart commented Oct 24, 2017

Closing this for now, feel free to reopen if you still have questions

@elihart elihart closed this as completed Oct 24, 2017
@BrandonJF
Copy link
Author

Hey, thank you so much. I used a combination of the above approaches and was able to succeed in getting things to work by taking your suggestion and having each model set its own color. When the next version is released with #330 I'll refactor so that I can set it on the linear layout via attribute. Thanks again for your help, as soon as I get done with this project I'll see where I can jump in and help!
Cheers,
Brandon.

@elihart
Copy link
Contributor

elihart commented Oct 25, 2017

Glad it worked 👍 I released the new version last night btw, hope it works for you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants