具有 WebView 数据成员的 PictureListener - 这有意义吗?
众所周知, PictureListener 是一个具有单个 public 的接口方法:onNewPicture(WebView视图,Picture图片)
。
它适用于简单的场景,但就我而言,我需要处理各种错误(由于互联网的不可预测性,特别是通过 WiFi/3G)。
为此,我打算创建一个稍微复杂的类,实现 PictureListener 并具有一些额外的状态信息和关联的处理程序函数。到目前为止一切顺利,但是...
其中一个函数需要访问 WebView
- 在 onNewPicture()
中作为参数传递的相同 WebView,但不是可用于其他任何地方的 PictureListener
。
因此,我正在考虑将该 WebView 作为参数传递给 MyPictureListener 的构造函数并将其保存为私有数据成员。
我以前没有见过这种实现的例子,我不确定我是否会违反我不熟悉的规则。
具有 WebView 数据成员的 PictureListener 有意义吗?我应该注意哪些陷阱吗?
As we all know, PictureListener is an interface with a single public method: onNewPicture(WebView view, Picture picture)
.
It works for simple scenarios but in my case I need to handle various errors (due to the predictably unpredictable nature of the Internet, especially via WiFi/3G).
For that, I intend to create a slightly more complex class, implementing PictureListener and having some extra state information and associated handler functions. So far so good, but...
One of the function needs access to the WebView
- the same WebView that is passed as a parameter in onNewPicture()
but isn't available to PictureListener
anywhere else.
So, I was thinking of passing that WebView as a parameter to the constructor of MyPictureListener
and saving it as a private data member.
I haven't seen an example of such implementation before and I am not sure whether I would be violating a rule with which I am not familiar.
Does a PictureListener that has a WebView data member make sense? Are there pitfalls that I should be aware of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,事实并非如此。
你不会违反规则,但你会违背 WebView 和 WebView 的“心态”。 PictureListener设计:
不接收 WebView 作为
范围。
关联的专用方法
PictureListener 就用它了。所以WebView
“了解”PictureListener。
无需将 WebView 参数传递给 PictureListener 的构造函数,只需将
onNewPicture()
中的view
参数强制转换为 WebView 并调用 WebView 中访问其中任何内容的方法即可。No, it doesn't.
You won't be violating rules but you will be going against the "mindset" of the WebView & PictureListener design:
doesn't receive WebView as a
parameter.
dedicated method for associating a
PictureListener with it. So WebView
"knows" about PictureListener.
Instead of passing a WebView parameter to PictureListener's constructor, just cast the
view
parameter inonNewPicture()
to your WebView and call a method in your WebView that access anything in it.