Android:如何最好地将数据传递到视图?

发布于 2024-10-16 00:57:34 字数 247 浏览 2 评论 0原文

我有一个显示游戏所有级别的视图。这些级别由活动读取,然后传递到视图中。我可以从视图中读取它们,但这实际上并不是它的责任,而且我喜欢关注点分离。

现在,我为此调用一个设置器:

((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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅浅淡淡 2024-10-23 00:57:34

这也是一个偏好问题。理论上来说,按照你现在的方式通过关卡是完全可以的。或者,如果您需要的不仅仅是设置级别,而是提供更多功能(即还保存级别),我通常使用一个单独的类负责处理这些事情(即存储库、一些“管理器”类等...)。然后,这个类被传递到构造函数上的视图中,最好是强制提供它。当然,为了分离事物,我使用接口而不是具体的实现,它可能看起来如下:

public class MyView {

   public MyView(ILevelLoader levelLoader){
      this.levelLoader = levelLoader;
   }

   ...
}

通常,这可能不起作用,因为视图是由框架直接实例化的,而不是由应用程序实例化的。在这种情况下,您被迫通过适当的设置者来完成此操作。这是某种 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:

public class MyView {

   public MyView(ILevelLoader levelLoader){
      this.levelLoader = levelLoader;
   }

   ...
}

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.

流年已逝 2024-10-23 00:57:34

我希望我没有错过重点,但这里是:
通常,您可以使用一个函数来设置某些内容(例如文本视图的文本),或者在 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'.

  • you can set it using setNumColumns (that would be the equivalent of your loadLevels() ? )
  • you can ignore it, it'll revert to default.
  • you can set it as an attribute lik so: app:numColumns="3"

You can try to use the attribute or the default in the class to accomplish this.

我不吻晚风 2024-10-23 00:57:34

使用抽象方法 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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文