- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
getBrands(); } 然后我有一个存储库来-6ren">
我有这样的改造服务
public interface BrandsService {
@GET("listBrand")
Call<List<Brand>> getBrands();
}
然后我有一个存储库来从这样的 api 获取数据
public class BrandsRepository {
public static final String TAG = "BrandsRepository";
MutableLiveData<List<Brand>> mutableLiveData;
Retrofit retrofit;
@Inject
public BrandsRepository(Retrofit retrofit) {
this.retrofit = retrofit;
}
public LiveData<List<Brand>> getListOfBrands() {
// Retrofit retrofit = ApiManager.getAdapter();
final BrandsService brandsService = retrofit.create(BrandsService.class);
Log.d(TAG, "getListOfBrands: 00000000000 "+retrofit);
mutableLiveData = new MutableLiveData<>();
Call<List<Brand>> retrofitCall = brandsService.getBrands();
retrofitCall.enqueue(new Callback<List<Brand>>() {
@Override
public void onResponse(Call<List<Brand>> call, Response<List<Brand>> response) {
mutableLiveData.setValue(response.body());
}
@Override
public void onFailure(Call<List<Brand>> call, Throwable t) {
t.printStackTrace();
}
});
return mutableLiveData;
}
}
我正在通过注入(inject) Retrofit 来使用 Dagger2 的构造函数注入(inject)。然后我有一个像这样的 ViewModel
public class BrandsViewModel extends ViewModel{
BrandsRepository brandsRepository;
LiveData<List<Brand>> brandsLiveData;
@Inject
public BrandsViewModel(BrandsRepository brandsRepository) {
this.brandsRepository = brandsRepository;
}
public void callService(){
brandsLiveData = brandsRepository.getListOfBrands();
}
public LiveData<List<Brand>> getBrandsLiveData() {
return brandsLiveData;
}
}
要在 BrandsRepository 中注入(inject) Retrofit,我必须像那样注入(inject) BrandsRepository。然后我有这样的 MainActivity
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Inject
BrandsViewModel brandsViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((MainApplication)getApplication()).getNetComponent().inject(this);
// BrandsViewModel brandsViewModel = ViewModelProviders.of(this).get(BrandsViewModel.class);
brandsViewModel.callService();
LiveData<List<Brand>> brandsLiveData = brandsViewModel.getBrandsLiveData();
brandsLiveData.observe(this, new Observer<List<Brand>>() {
@Override
public void onChanged(@Nullable List<Brand> brands) {
Log.d(TAG, "onCreate: "+brands.get(0).getName());
}
});
}
}
BrandsViewModel 是使用 Dagger2 而不是 ViewModelProviders 注入(inject)的。这工作正常但是当我尝试通过取消注释来使用 ViewModelProviders 时,dagger 给我一个明显的错误。获取 ViewModel 的正确方法是使用 ViewModelProviders,但我将如何在注入(inject)这样的改造时实现这一点。
最佳答案
编辑:重要说明。要使用 Jetpack ViewModel,您不需要 map-multibinding。继续阅读。
答案可能比 Mumi 的方法更简单,即在组件上公开 ViewModel:
@Singleton
@Component(modules={...})
public interface SingletonComponent {
BrandsViewModel brandsViewModel();
}
现在您可以在 ViewModelFactory 中的组件上访问此方法:
// @Inject
BrandsViewModel brandsViewModel;
...
brandsViewModel = new ViewModelProvider(this, new ViewModelProvider.Factory() {
@Override
public <T extends ViewModel> create(Class<T> modelClazz) {
if(modelClazz == BrandsViewModel.class) {
return singletonComponent.brandsViewModel();
}
throw new IllegalArgumentException("Unexpected class: [" + modelClazz + "]");
}).get(BrandsViewModel.class);
所有这些都可以使用 Kotlin 进行简化和隐藏:
inline fun <reified T: ViewModel> AppCompatActivity.createViewModel(crossinline factory: () -> T): T = T::class.java.let { clazz ->
ViewModelProvider(this, object: ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if(modelClass == clazz) {
@Suppress("UNCHECKED_CAST")
return factory() as T
}
throw IllegalArgumentException("Unexpected argument: $modelClass")
}
}).get(clazz)
}
现在可以让你做
brandsViewModel = createViewModel { singletonComponent.brandsViewModel() }
现在在哪里BrandsViewModel
可以从 Dagger 接收它的参数:
class BrandsViewModel @Inject constructor(
private val appContext: Context,
/* other deps */
): ViewModel() {
...
}
尽管如果 Provider<BrandsViewModel>
的 Intent 可能更清晰而是从 Dagger 暴露
interface SingletonComponent {
fun brandsViewModel(): Provider<BrandsViewModel>
}
brandsViewModel = createViewModel { singletonComponent.brandsViewModel().get() }
来自 android-ktx
的一些额外技巧, 你甚至可以做
@Suppress("UNCHECKED_CAST")
inline fun <reified T : ViewModel> Fragment.fragmentViewModels(
crossinline creator: () -> T
): Lazy<T> {
return createViewModelLazy(T::class, storeProducer = {
viewModelStore
}, factoryProducer = {
object : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(
modelClass: Class<T>
): T = creator.invoke() as T
}
})
}
然后
class ProfileFragment: Fragment(R.layout.profile_fragment) {
private val viewModel by fragmentViewModels {
singletonComponent.brandsViewModelFactory().get()
}
在哪里brandsViewModelFactory()
是
fun brandsViewModelFactory(): Provider<BrandsViewModel>
关于android - 带有 Dagger 2 的 ViewModelProviders,无法掌握概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50673266/
我看到两个名称相似的类,ViewModelProviders 和 ViewModelProvider。任何人都可以解释这些类(class)之间的区别是什么?哪个类实际提供了 ViewModel? 最佳
我正在尝试创建可感知生命周期的 View 模型。但是我在我的 Android 项目中找不到 ViewModelProviders 类,只有 ViewModelProvider。似乎也没有 androi
我目前正在将我的项目架构从 MVP 转换为 MVVM。当我在研究它时,我发现有些事情让我感到困惑: 在项目iosched的ScheduleViewModelFactory.kt中,工厂实现ViewMo
由于所说的贬值,我不再使用 ViewModelProviders.of(this)。我研究了其他问题并使用了答复中的代码。所有代码都没有错误,但当我尝试运行该应用程序时,出现异常。 此处发生错误。 P
我有一个在我的“MainActivity.java”上启动的模型,其名称如下: //MainActivity.java (public class MainActivity extends AppCo
我正在研究 Android ViewModel 架构组件,但在尝试在 AppCompatActivity 中初始化 ViewModel 时出现上述错误。 import android.arch.lif
“ViewModelProviders”已从 androidx.lifecycle:lifecycle-*:2.2.0-alpha03 中弃用 https://developer.android.co
我想重新使用 ViewModel 和 LiveData 从 Firebase 读取节点。这是我在 Fragment 中的代码 FirebaseDatabaseViewModel tes
我正在尝试使用 View 模型,但是遇到了一个奇怪的错误,编译器说它无法识别“of”。我已经在我的 build.gradle 中导入了依赖项并像这样同步 def ver = "1.1.1" impl
我使用 ViewModelProviders。但是今天,更新库后,有30条消息。据我所知,ViewModelProviders 不再可用,您需要替换它,我查看了 developer.google,但不
我正在尝试遍历 google tutorial on building good ap architecture .我正在使用 Java 8 和 SDK 26。这是我到目前为止的代码: package
我试图通过将我的一些 ViewModel 实例移动到一个抽象基类中来减少 Dagger 的一些样板,但找不到一个很好的方法来做到这一点。我的 Intent 是从我的基本 fragment 中实例化我的
ActivityViewModels 和惰性 ViewModelProvider 的区别? 我见过两种初始化 View 模型的方法: private val someViewModel: SomeVi
为什么我应该为 View 模型使用 viewmodelproviders? 为什么我不能将自定义单例注释添加到我的 View 模型中,然后将此 View 模型注入(inject) fragment 类
这就是我正在努力做的事情。 在 Fragment 内设置对象的 ArrayList 从 FragmentActivity 中的观察者获取该数组容器(承载所有 fragment 的 Activity)
我相信我正在遵循为我提供的用于建立 ViewModelProvider.Factory 的代码示例。它发生在 OnAttach 方法的 CheckInRecentList 代码中的这条语句上。我们将不
关于过去的问题How to get a ViewModel? ,以及我发现的例子,它并不能解决我的问题。我没有在那里发表评论的权限,所以我不得不再次询问。我没有 ViewModelProviders
我是 Kotlin 的新手,请帮助我如何在 Kotlin 中使用 ViewModelProviders.of(this) 我的java代码是 mFavViewModel = ViewModelPro
嘿,我正在尝试让我的 ViewModel 工作,但到目前为止还没有运气。Android Studio 显示错误 Cannot resolve symbol 'ViewModelProviders'。
我很高兴使用新的 Android 架构组件 ViewModel 系统,它很好地将 Activity/Fragment/Layout 渲染问题与 ViewModel 逻辑分开。我已经成功地对 ViewM
我是一名优秀的程序员,十分优秀!