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

Documentation or implementation bug. Custom ViewHolder cannot be cast to eu.davidea.viewholders.FlexibleViewHolder #117

Closed
ivankarpey opened this issue Jun 17, 2016 · 5 comments

Comments

@ivankarpey
Copy link

ivankarpey commented Jun 17, 2016

Issue is correct for the current snapshot.

I'm dealing with sticky headers. Nowhere in docs mentioned, that instead of custom ViewHolder inheritance there should be FlexibleViewHolder used. And generic constraints of interfaces of provided abstract classes, like AbstractHeaderItem and AbstractSectionableItem expect to get classes inherited from RecyclerView.ViewHolder

However StickyHeaderHelper class's method getHeaderViewHolder(int position) have direct casting to FlexibleViewHeader and of course will failed if there is simple ViewHolders used with error similar to this one:

FATAL EXCEPTION: main
Process: test, PID: 29504
java.lang.ClassCastException: Fragments.HeaderViewHolder cannot be cast to eu.davidea.viewholders.FlexibleViewHolder
at eu.davidea.flexibleadapter.helpers.StickyHeaderHelper.getHeaderViewHolder(StickyHeaderHelper.java:266)
at eu.davidea.flexibleadapter.helpers.StickyHeaderHelper.updateHeader(StickyHeaderHelper.java:153)
at eu.davidea.flexibleadapter.helpers.StickyHeaderHelper.updateOrClearHeader(StickyHeaderHelper.java:143)
at eu.davidea.flexibleadapter.helpers.StickyHeaderHelper.initStickyHeadersHolder(StickyHeaderHelper.java:122)
at eu.davidea.flexibleadapter.helpers.StickyHeaderHelper.access$000(StickyHeaderHelper.java:40)
at eu.davidea.flexibleadapter.helpers.StickyHeaderHelper$1.run(StickyHeaderHelper.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
 at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

So i think either it's need to fix docs and probably introduce new abstractions which will be statically checking correct typing, or fix the code to not assume it's use FlexibleViewHolder items.

@davideas davideas added the bug label Jun 18, 2016
@davideas
Copy link
Owner

davideas commented Jun 18, 2016

Hello @ivankarpey, thank you for your message.
Indeed that is still not ok, I introduced this constraint as temporary fix for crashing bugs since 2-3 snapshots ago, to allow people continue to develop. Before next beta7, I have to find a final solution, that is good for everybody. I cannot put this in documentation because it is not closed yet.

However, thank you that you have spotted this, I'm going to fix this.

For documentation, the wiki page Headers and Sections clearly explain that sections are identified by IHeader and ISectionable interfaces.
New and better docs will follow.

@davideas
Copy link
Owner

@ivankarpey, I refactored the item interfaces signatures to be compliant with StickyHeaderHelper.

@ivankarpey
Copy link
Author

Thanks. Looks like docs much clearer now. I'm plan to use flexible adapter pretty aggressively in my app and will try to provide some more improvements if will find any.

@jinsen47
Copy link

jinsen47 commented Sep 2, 2016

Got same exception when my header class extends AbstractHeaderItem and normal item extends AbstractSectionableItem. HELP IN NEED.
I do as the doc said, but still got a ClassCastException, with 5.0.0-b7

@davideas
Copy link
Owner

davideas commented Sep 2, 2016

@jinsen47, can you check if getLayout() of both classes returns a different layout?
If not resolved, can you paste here the 2 items and the stacktrace you get?

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

No branches or pull requests

3 participants