- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Assets 文件夹中有一个 JSON 文件,DataManager(存储库)类需要它,因此 Assets 管理器(和上下文)应该可以访问 Assets 。
问题是,根据最佳实践,Android 上下文或 Android 特定代码不应传递到数据层(ViewModel-Repo-Model),因为编写单元测试等很容易,而且 View 不应与数据交互直接分层。
我最终提供了使用列表并将其注入(inject)存储库。
这是正确的做法吗?
-谢谢
P.S: 我的模块类提供了列表
@Module
public class UtilModule {
@Provides
@JsonScope
JsonUtil provideJsonUtil(AssetManager assetManager){
return new JsonUtil(assetManager);
}
@Provides
@JsonScope
String provideJson(JsonUtil util){
return util.getJson();
}
@Provides
@JsonScope
Type provideType(){
return new TypeToken<List<Data>>() {}.getType();
}
@Provides
@JsonScope
DataManager provideDataManager (Gson gson, Type type,String json) {
return new DataManager (gson.fromJson(json, type));
}
}
最佳答案
ViewModel
和/或 Repository
访问 Application
并不违反 MVVM。直接访问上下文,这就是访问 AssetsManager
所需的全部内容。调用 Application.getAssets()
是可以的,因为 ViewModel
不使用任何特定的 Activity
的上下文。
例如,您可以使用 Google 提供的 AndroidViewModel
子类而不是父类(super class) ViewModel
. AndroidViewModel
在其构造函数中获取一个 Application
(ViewModelProviders
将为您注入(inject))。您可以在其构造函数中将您的 Application
传递给您的 Repository
。
或者,您可以使用 Dagger依赖注入(inject)将 Application
直接注入(inject)您的 Repository
。 (注入(inject) Application
上下文有点棘手。请参阅 Dagger 2 injecting Android Context 和 this issue filed on the Danger github repo。)如果你想让它变得非常光滑,你可以为 AssetManager
配置一个提供者并将其直接注入(inject)您的 Repository
。
最后,如果您正在使用 Room,并且您想要的只是使用存储在 Assets 中的预配置数据库预填充 Room 数据库,您可以按照此处的说明进行操作:How to use Room Persistence Library with pre-populated database?
关于android - android中的MVVM,在不破坏模式的情况下访问assetManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54503406/
public static AssetManager assets = new AssetManager(); 应该避免,但是如何在多个屏幕中处理 AssetManager?这是一个好的解决方案吗?
以下代码生成一个没有条目的列表: AssetManager am = getContext().getAssets(); try { String[] xmls = am.l
两个版本都可以使用,但我不确定使用哪个版本以及为什么要使用那个版本?如果您已经卸载了 Assets ,是否需要处置 TextureAtlas? 版本 1:加载包文件(带有位置数据的 TexturePa
我正在尝试使用 AssetManager 加载我的 libgdx 游戏的关卡图像,从我遵循的指南来看这似乎很容易,但我遇到了某种错误.. 我在名为 Assets.java 的类中使用此代码包加载器;
我正在使用 assetManager 加载各种 Assets 。但是当我从应用程序中“返回按钮”并再次加载它时,在我的 show() 方法 while (!Assets.manager.update(
我在选择墙纸时遇到了异常。我实际上不确定罪魁祸首是什么功能。这是错误日志: I/ActivityManager( 1360): Starting activity: Intent { c
我正在阅读 Mario Zechner 的“开始 Android 游戏”一书,很高兴我拿起了它,但我现在遇到了一个问题,他要求用户在早期编写代码的“测试”之一这本书。并不是说我反对对它们进行编码,我宁
我试图让 AssetManager 形成 Android 库项目中的一个类,但出现错误: Caused by: java.lang.NullPointerException: Attempt to i
是否可以在没有上下文的情况下访问 AssetManager?我看到其他人的解决方案是扩展Application,并将Context保存在onCreate()中,并创建一个静态getter来返回Cont
所以我在 LibGDX 中制作游戏,我使用 AssetManager 加载我的所有 Assets 。 我只是不确定哪种是正确的使用方法。 目前我正在第一个屏幕之前加载所有游戏 Assets 。 然后我
MyGdxGame.java public void print(){ manager=new AssetManager(); manager.load("selectlevel.p
尝试创建加载屏幕,我有一个 Assets 类和一个 LoadingScreen 类,该类立即从以下位置调用: this.setScreen(new LoadingScreen(this)); 在加载屏
我正在使用 Libgdx assetmanager 加载 UI 资源。它工作得很好,直到我重新启动应用程序(Android)。发生的情况如下: 我开始游戏:运行良好 我使用 Gdx.app.exit(
我在 libgdx 中的 AssetManager 遇到问题,我在主类中创建它,并有一个 getMethod 将 assetManager 返回到我的屏幕。当我在屏幕类中转到 assetManager
这个问题已经有答案了: Android - The local variable tst may not have been initialized (7 个回答) 已关闭10 年前。 我有一个类,我
我正在使用 Java 中的 assetmanager 打开一个文件以在我的本地环境中读取。我希望在初始化时只打开这个文件一次,然后多次使用文件中定义的函数。为此,我需要将 assetmanager 保
进行 Android 2.1 开发。任何人都可以向我解释为什么以下代码生成 IOException 并且不加载文件吗?这个确切的代码曾经有效,据我所知,它应该仍然有效。作为引用,Log.d() 命令正
我有 2 个类,来自 BinderData 类,它扩展了 BaseAdapter(我不能将此类扩展到 Activity,因为我必须扩展到 BaseAdapter)我正在通过以下代码调用类 AssetA
我正在 libgdx 中开发平台游戏。我使用 AssetManager 类将我所有的 Assets 加载到游戏中。所以,我的问题是:我是否需要处理我的纹理和图集,即使我正在使用 Assets 管理器卸
我正在尝试将 AssetManager.list() 的功能用于游戏,但一次调用需要花费大量时间(~0.5-1.0 秒)。更糟糕的是,我能找到的 AssetManager 的唯一来源列出了以下定义,没
我是一名优秀的程序员,十分优秀!