Solution 1 :
This works in most cases because most ViewParent
s are ViewGroup
s.
ViewGroup
extends View
and implements ViewParent
, so it satisfies both.
The reason that ViewParent
is a separate interface is that this won’t always work: if you keep going up the tree, you’ll eventually find a ViewParent
that is not a ViewGroup
(specifically, you’ll get to the ViewRootImpl
), and thus also not a View
, and that will result in a ClassCastException
.
Problem :
This code:
(View) contentView.getParent();
I don’t quite get it: ViewParent is an interface, View is a class that doesn’t implement that interface.
How do they transform into one another?
Comments
Comment posted by shadow_wxh
It’s also interesting that by accessing the inferface you actually accessing the the whole class that inplements the interface,I guess that is just the way it is.
Comment posted by Ryan M
Well, that’s what the cast is for: just accessing the interface along doesn’t get you the whole class. You have to be explicit that you know that it’s