carrot / recycler-core

Multiple view type handling in RecyclerViews with 90% less tears
25 stars 1 forks source link

Multiple models in a RecyclerView #46

Closed Gonzom closed 8 years ago

Gonzom commented 8 years ago

Hey,

I was requested to create a layout of a vertical RecyclerView with rows of nested horizontal RecyclerViews. Each nested row has a ContactModel in the first position. This class has several attributes, one of them is "type" which can be "user" or "group". A group has a different layout than a user.

The rest of the list are ActivityModel items.

I've created the models and controllers for each item, but I'm stuck on 2 fronts.

1) How to combine both items into 1 list - I could create a BaseItem and have both ContactModel and ActivityModel inherit from it. Would the controller know that a current item is an instance of one of the classes?

2) If 1 would work, how will I be able to change the layout of ContactModel depending on its type?

Would appreciate any assistance here.

Thanks,

I've attached class files Archive.zip

roideuniverse commented 8 years ago

@Gonzom There are a couple of things that need to be fixed. This does not look like a problem in Recycler Core. I will change your code and update a working code as soon as I have some free time.

  1. The Models and controllers are mapped using, @InjectController(controller = ContactController.class, layout = R.layout.row_nested_floatfeed_user) so the models are free to inherit from any class.
  2. Model <-> Controller is a 1-1 mapping. I see that you have two models that extend from same controller, we should not do that. Every model should have its own controller.
  3. Since we use annotations to bind the models with Controller, we don't need to the type of the model from the Adapter/RecyclerView perspective. You cannot and should not change the layout of one model or ContactModel. If your view is different, you should have a different model and a controller.

As a workaround, you can create another model that extends the same model, so it will retain all the same methods and fields, and you can respective controllers and models for it.

roideuniverse commented 8 years ago

@Gonzom Fixed your code. This should get you started. Archive.zip

So closing this issue. If this does not help you, feel free to open an issue again.