Casting exceptions when using groups
See original GitHub issueHi, I have a fairly complex use case where I have 4 kinds of posts (simple message, photo, video, embedded link). All of this posts share in common a header, a text section that’s where the user writes down a message and a comments section.
I decided it would be better to separate each section into its own model (so I have HeaderModel_, TextContentModel_, PhotoModel_, VideoModel_, EmbeddedLinkModel_ and CommentsModel_). Each model is created using the @ModelView
attribute, inflates a layout and has it’s own properties and requirements (the PhotoModel_ for example has a photoUrl
property, while the video one has a videoUrl
, while the embedded link model has url
, previewPhotoUrl
, etc).
I was planning on grouping them because all this models need to be put together given the post type and represented inside a CardView
, so I have also a class called PostGroup (which extends from EpoxyModelGroup
) #where I discuss the kind of post received and given the outcome I build the full post as a group (so, If it’s a photo post I would add
to the group HeaderModel_, TextContentModel_, PhotoModel_ and CommentsModel_). For this, my group layout has 4 ViewStubs (I always end up adding 4 models regardless of the post).
This approach sadly seems to fail with a ClassCastException
stating that, PhotoModel_ can’t be casted to VideoModel_, or that VideoModel_ can’t be casted to EmbeddedLinkModel_, etc.
Any reasons why this approach might fail? Should I separate each type of post on it’s own group even if mostly all of them display the same amount of Models? Each group being added to the adapter has it’s own Id (I doubled checked this). If not this one, which approach should I follow to be able to do what I stated above? (grouping posts given its type so they can be rendered inside a CardView).
Here’s an shortened example of the group layout I’m using:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ViewStub
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ViewStub
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ViewStub
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ViewStub
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Thanks!
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (6 by maintainers)
I would like to on this and https://github.com/airbnb/epoxy/issues/384 soon
@mradzinski You might not need a separate group class. In my case I use one group and assign the layout dynamically based on type
You actually can override view type already with the
getViewType
method. If you want you could just return a unique id there based on your item type and use the same layout file for everything.The nested recycling would be a bit trickier. I think it would require something like this:
This seems very doable, but definitely some complexity. We only use groups in one place so I haven’t prioritized it. It would be cool though, and I’m open to pull requests!