Android:如何最好地将数据传递到视图?
我有一个显示游戏所有级别的视图。这些级别由活动读取,然后传递到视图中。我可以从视图中读取它们,但这实际上并不是它的责任,而且我喜欢关注点分离。
现在,我为此调用一个设置器:
((GameView) findViewById(R.id.game)).setLevels(loadLevels());
但是,我不喜欢这样一个事实:如果我忘记调用设置器,视图将无法正常工作。有没有更好的方法来通过级别?
I have a view that displays all the levels of my game. These levels are read by the activity and then passed into the view. I could read them from the view, but it's not really its responsibility, and I'm a fan of separation of concerns.
Right now, I'm calling a setter for this:
((GameView) findViewById(R.id.game)).setLevels(loadLevels());
However, I don't like the fact that the view will be dysfunctional if I forget to call the setter. Is there a better way to pass the levels in?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这也是一个偏好问题。理论上来说,按照你现在的方式通过关卡是完全可以的。或者,如果您需要的不仅仅是设置级别,而是提供更多功能(即还保存级别),我通常使用一个单独的类负责处理这些事情(即存储库、一些“管理器”类等...)。然后,这个类被传递到构造函数上的视图中,最好是强制提供它。当然,为了分离事物,我使用接口而不是具体的实现,它可能看起来如下:
通常,这可能不起作用,因为视图是由框架直接实例化的,而不是由应用程序实例化的。在这种情况下,您被迫通过适当的设置者来完成此操作。这是某种 MVC/MVP 模式。
出于您的兴趣,您可能还想了解一下 IoC 容器 和依赖项注入。 Google 提供的 Guice 是一个很好的框架,我已经在 Android 上使用过。
It is also a bit a matter of preference. Theoretically it's perfectly fine to pass the levels as you're doing. Alternatively, if you need more than just set the levels, but provide further functionalities (i.e. also saving of levels) I normally use a separate class responsible for handling such things (i.e. a Repository, some "Manager" class etc...). This class is then passed into the View on the constructor preferably s.t. one is forced to provide it. Of course, in order to separate things, I use interfaces rather than specific implementations s.t. it may then look as follows:
Often, this may not work, because the view is something instantiated by the framework directly rather than by the application. In such a situation you're forced to do it through an appropriate setter. It is some sort of MVC/MVP pattern.
Just for your interest, you might also want to take a look at IoC containers and dependency injection. Guice provided by Google is a nice framework I've already used on Android.
我希望我没有错过重点,但这里是:
通常,您可以使用一个函数来设置某些内容(例如文本视图的文本),或者在 xml 中设置一个属性。
看看我在一个问题上得到的答案:如何在屏幕中央布局图像“网格”
自定义视图需要一些东西,但让我们举个例子:“numColumns”。
setNumColumns
设置它(这相当于您的 loadLevels() ?)app:numColumns="3"
您可以尝试使用该属性或类中的默认值来完成此操作。
I hope I didn't miss the point, but here goes:
Generally you have either a function setting something (like the text for a textview), or an attribute you set in the xml.
Take a look over at this answer I got on a question: How to layout a 'grid' of images in the center of the screen
There are some things the custom view needs, but lets take an example: 'numColumns'.
setNumColumns
(that would be the equivalent of your loadLevels() ? )app:numColumns="3"
You can try to use the attribute or the default in the class to accomplish this.
使用抽象方法 getLevels() 使您的视图成为抽象类?这样,当您实例化该类时,如果您忘记传递代码中的级别,则将无法编译。
我想这是否更好是一个品味问题:)
Make your view an abstract class with an abstract method
getLevels()
? This way, when you instantiate the class if you forget to pass the levels in your code won't compile.Whether or not this is better is a matter of taste I guess :)