如何在没有 ViewModelProviders class 的情况下获得 ViewModel?
How do I get a ViewModel without ViewModelProviders class?
关于过去的问题,以及我找到的例子,它没有解决我的问题。我没有在那里发表评论的权限,所以我不得不再次询问。我没有 ViewModelProviders class,我想在我找到的所有示例和文档中使用 "of" 方法,例如
model = ViewModelProviders.of(this).get(Something.class);
在 Android Studio 中,我找不到 "android.arch.lifecycle:extensions" 添加为依赖项。当我搜索时它说 "nothing to show" 。我在 API 27。
所以,尝试使用未弃用的元素,我在片段中有这个:
public class EventDetailsFragment extends Fragment
{
EventViewModel viewModel;
@Override
public void onActivityCreated (Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
ViewModelProvider.Factory factory = ViewModelProvider.AndroidViewModelFactory.getInstance(getActivity().getApplication());
// HERE IS THE ISSUE: Seems not good practice to have to create a new ViewModelProvider every time this Fragment is created.
// Perhaps I should just create a singleton ViewModelProvider in the Activity or Application,
// so here could call getActivity().getViewModelProvider(this, factory).
ViewModelProvider viewModelProvider = new ViewModelProvider(this, factory);
viewModel = viewModelProvider.get(EventViewModel.class);
...
}
我该怎么办?
打开 app/build.gradle
,找到 dependencies
块并手动将生命周期依赖项添加到列表中:
dependencies {
implementation 'android.arch.lifecycle:extensions:1.1.1'
// ...
}
同步项目,之后您应该能够获得 ViewModel 的实例。
如果您对 Android 和 Java 有更好的了解,请尝试学习一些 MVVM
架构。将此代码放入一些 baseFragment
方法中,然后 make:
abstract class BaseFragment extends Fragment() {
//kotlin
fun <T : ViewModel?> getViewModel(activity: FragmentActivity, modelClass: Class<T>): T {
return ViewModelProviders.of(this, ViewModelFactory(activity)).get(modelClass)
}
//bytecode to java is this :/
public final ViewModel obtainViewModel(@NotNull FragmentActivity activity, @NotNull Class modelClass) {
return ViewModelProviders.of((Fragment)this, (Factory)(new
ViewModelFactory((Activity)activity))).get(modelClass);
}
}
也不要将此代码放入 onViewCreated
而不是放在 onActivityCreated
// kotlin
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = obtainViewModel(requireActivity(), KebabFragmentViewModel::class.java)
}
// java bytecode
public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.viewModel = (RollerFragmentViewModel)this.obtainViewModel(getFragmentActivity(), RollerFragmentViewModel.class);
}
现在你需要观察者从 viewModel 中的 onViewCreated mby 中获取一些东西,用于一些可能的网络操作或一些可能需要更长时间的东西
// kotlin
viewModel.list.observe(this, Observer { list ->
list?.let {
viewModel.myKebabAdapter.updateList(list)
}
})
//java
this.viewModel.getErrorMessage().observe((LifecycleOwner)this, (Observer)(new Observer() {
...
public final void onChanged(@Nullable List list) {
this.viewModel.myKebabAdapter.updateKebabList(list)
}
}
并且在 viewModel 中,您获得了用于在 activity/fragment
上观察的可爱变量
// kotlin
var list: MutableLiveData<MutableList<User>> = MutableLiveData()
// java variable and getter
@NotNull
private MutableLiveData userList;
@NotNull
public final MutableLiveData getUserList() {
return this.userList;
}
现在你需要 dependencies{}
块中的依赖项 app/build.gradle
//lifecycle
implementation "android.arch.lifecycle:extensions:1.1.1"
implementation "android.arch.lifecycle:viewmodel:1.1.1"
// RxJava
implementation "io.reactivex.rxjava2:rxkotlin:2.2.0"
implementation "io.reactivex.rxjava2:rxandroid:2.0.0"
implementation "io.reactivex.rxjava2:rxjava:2.1.10"
查找一些博客教程,了解如何正确执行此操作。我个人不喜欢 https://developer.android.com/ 它曾经很难理解。试试中号。
GL。 Java 大部分代码都经过反编译,希望它能正常工作。
关于过去的问题
model = ViewModelProviders.of(this).get(Something.class);
在 Android Studio 中,我找不到 "android.arch.lifecycle:extensions" 添加为依赖项。当我搜索时它说 "nothing to show" 。我在 API 27。 所以,尝试使用未弃用的元素,我在片段中有这个:
public class EventDetailsFragment extends Fragment
{
EventViewModel viewModel;
@Override
public void onActivityCreated (Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
ViewModelProvider.Factory factory = ViewModelProvider.AndroidViewModelFactory.getInstance(getActivity().getApplication());
// HERE IS THE ISSUE: Seems not good practice to have to create a new ViewModelProvider every time this Fragment is created.
// Perhaps I should just create a singleton ViewModelProvider in the Activity or Application,
// so here could call getActivity().getViewModelProvider(this, factory).
ViewModelProvider viewModelProvider = new ViewModelProvider(this, factory);
viewModel = viewModelProvider.get(EventViewModel.class);
...
}
我该怎么办?
打开 app/build.gradle
,找到 dependencies
块并手动将生命周期依赖项添加到列表中:
dependencies {
implementation 'android.arch.lifecycle:extensions:1.1.1'
// ...
}
同步项目,之后您应该能够获得 ViewModel 的实例。
如果您对 Android 和 Java 有更好的了解,请尝试学习一些 MVVM
架构。将此代码放入一些 baseFragment
方法中,然后 make:
abstract class BaseFragment extends Fragment() {
//kotlin
fun <T : ViewModel?> getViewModel(activity: FragmentActivity, modelClass: Class<T>): T {
return ViewModelProviders.of(this, ViewModelFactory(activity)).get(modelClass)
}
//bytecode to java is this :/
public final ViewModel obtainViewModel(@NotNull FragmentActivity activity, @NotNull Class modelClass) {
return ViewModelProviders.of((Fragment)this, (Factory)(new
ViewModelFactory((Activity)activity))).get(modelClass);
}
}
也不要将此代码放入 onViewCreated
而不是放在 onActivityCreated
// kotlin
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = obtainViewModel(requireActivity(), KebabFragmentViewModel::class.java)
}
// java bytecode
public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.viewModel = (RollerFragmentViewModel)this.obtainViewModel(getFragmentActivity(), RollerFragmentViewModel.class);
}
现在你需要观察者从 viewModel 中的 onViewCreated mby 中获取一些东西,用于一些可能的网络操作或一些可能需要更长时间的东西
// kotlin
viewModel.list.observe(this, Observer { list ->
list?.let {
viewModel.myKebabAdapter.updateList(list)
}
})
//java
this.viewModel.getErrorMessage().observe((LifecycleOwner)this, (Observer)(new Observer() {
...
public final void onChanged(@Nullable List list) {
this.viewModel.myKebabAdapter.updateKebabList(list)
}
}
并且在 viewModel 中,您获得了用于在 activity/fragment
// kotlin
var list: MutableLiveData<MutableList<User>> = MutableLiveData()
// java variable and getter
@NotNull
private MutableLiveData userList;
@NotNull
public final MutableLiveData getUserList() {
return this.userList;
}
现在你需要 dependencies{}
块中的依赖项 app/build.gradle
//lifecycle
implementation "android.arch.lifecycle:extensions:1.1.1"
implementation "android.arch.lifecycle:viewmodel:1.1.1"
// RxJava
implementation "io.reactivex.rxjava2:rxkotlin:2.2.0"
implementation "io.reactivex.rxjava2:rxandroid:2.0.0"
implementation "io.reactivex.rxjava2:rxjava:2.1.10"
查找一些博客教程,了解如何正确执行此操作。我个人不喜欢 https://developer.android.com/ 它曾经很难理解。试试中号。
GL。 Java 大部分代码都经过反编译,希望它能正常工作。