Android 中的 onSizeChanged/自定义视图类
我需要一个扩展视图的类来重写 onSizeChanged 方法,以便跟踪我的视图元素之一的大小。我有另一个类来管理这个视图,我想让那个类接受我的视图作为视图,而不是大小视图。本质上,我想让我重写 onSizeChanged 方法对类(管理类)的用户透明。
当我的管理类接受视图时,我需要将视图转换为我的自定义视图类。不幸的是,当我这样做时,我得到了一个类广播异常。
这是我失败的功能。另外, this.root 的类型是 SizeView
public void setContentView(View view) {
this.root = (SizeView) view;
this.window.setContentView(view);
}
可以这样做吗?或者,监视我的视图变量之一的 onSizeChanged 方法的最佳方法是什么?
谢谢!
I need to have a class which extends view that overrides the onSizeChanged method in order to keep track of the size of one of my view elements. I have another class which manages this view and I'd like to have that class accept my view as a View, not as a Sized view. Essentially, I'd like to make the fact that I'm override the onSizeChanged method transparent to the user of the class (of the managing class).
When my management class accepts the view, I need to cast the View to my custom view class. Unfortunately when I do this I get a classcastexception.
This is the function where I am failing. Also, this.root is of type SizeView
public void setContentView(View view) {
this.root = (SizeView) view;
this.window.setContentView(view);
}
Is it possible to do this? Or alternatively, what is the best way to monitor the onSizeChanged method of one of my view variables?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保您传入的变量
view
已初始化为SizeView
。有关转换的更多信息请参见此处。您还应该在投射之前检查
view
是否实际上是SizeView
类型。因此,请使用view instanceof SizeView
,然后如果它是 true 则进行转换。Make sure the variable
view
that you pass in was initialized as aSizeView
. A bit more information on casting here.You should also check whether
view
is actually of typeSizeView
before casting it. So useview instanceof SizeView
and then if it is true do your cast.