我正在MVVM和Dagger 2中运行小型Android应用程序。但是当我只有一个Activity和两个Fragments时,我不知道如何正确使用Dagger 2。这两个片段都是ViewModels的所有者。我已经将ViewModelProvider注入Fragment,但是我对此解决方案仍然感到困惑。也许有人会改善我的代码?
活动:
public class MainActivity extends DaggerAppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navView = findViewById(R.id.nav_view);
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupWithNavController(navView, navController);
}
}
FirstFragment :
public class EventsFragment extends DaggerFragment {
private static final String TAG = "EventsFragment";
private EventsViewModel eventsViewModel;
private final EventsAdapter adapter = new EventsAdapter();
private List<Event> events = new ArrayList<>();
@Inject
ViewModelProviderFactory viewModelProviderFactory;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
eventsViewModel = ViewModelProviders.of(this, viewModelProviderFactory).get(EventsViewModel.class);
View root = inflater.inflate(R.layout.fragment_events, container, false);
RecyclerView recyclerView = root.findViewById(R.id.events_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
getEvents();
return root;
}
第二个片段:
public class ScheduleFragment extends DaggerFragment {
private ScheduleViewModel scheduleViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
scheduleViewModel = ViewModelProviders.of(this).get(ScheduleViewModel.class);
View root = inflater.inflate(R.layout.fragment_schedule, container, false);
return root;
}
@Override
public void onAttach(Context context) {
AndroidSupportInjection.inject(this);
super.onAttach(context);
}
}
在这种情况下,两个片段都可以工作,但是我只能将ViewModelFactory注入其中一个。如果我将其注入第二个对象:错误:找不到符号类DaggerAppComponent
ActivityBuildersModule:
@Module
public abstract class ActivityBuildersModule {
@ContributesAndroidInjector(modules = FragmentBuildersModule.class)
abstract MainActivity contributeMainActivity();
}
AppComponent:
@Singleton
@Component(
modules = {
AndroidSupportInjectionModule.class,
ActivityBuildersModule.class,
AppModule.class,
ViewModelFactoryModule.class
}
)
public interface AppComponent extends AndroidInjector<BaseApplication> {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(Application application);
AppComponent build();
}
}
FragmentBuildersModule:
@Module
public abstract class FragmentBuildersModule {
@ContributesAndroidInjector(
modules = {ViewModelModule.class, EventsModule.class}
)
abstract EventsFragment contributeEventsFragment();
@ContributesAndroidInjector(
modules = {ViewModelModule.class}
)
abstract ScheduleFragment contributeScheduleFragment();
}
ViewModelFactory:
@Module
public abstract class ViewModelFactoryModule {
@Binds
public abstract ViewModelProvider.Factory bindViewModelFactory(ViewModelProviderFactory modelProviderFactory);
}
ViewModelKey:
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@MapKey
public @interface ViewModelKey {
Class<? extends ViewModel> value();
}
ViewModelModule:
@Module
公共抽象类ViewModelModule {
@Binds
@IntoMap
@ViewModelKey(EventsViewModel.class)
public abstract ViewModel bindEventsViewModel(EventsViewModel eventsViewModel);
@Binds
@IntoMap
@ViewModelKey(ScheduleViewModel.class)
public abstract ViewModel bindScheduleViewmodel(ScheduleViewModel scheduleViewModel);
BaseApplication:
public class BaseApplication extends DaggerApplication {
@Override
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
return DaggerAppComponent.builder().application(this).build();
}
答案 0 :(得分:1)
就像在第一个片段中那样做。 试试这个
public class ScheduleFragment extends DaggerFragment {
private ScheduleViewModel scheduleViewModel;
@Inject
ViewModelProviderFactory viewModelProviderFactory;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
scheduleViewModel = ViewModelProviders.of(this, viewModelProviderFactory).get(ScheduleViewModel.class);
View root = inflater.inflate(R.layout.fragment_schedule, container, false);
return root;
}
}
还有一种通过在AppModule中添加这种方法来提供Api的方法
@Singleton
@Provides
public RemoteApi provideRemoteApi(){
return Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(Constants.API_BASE_URL)
.build()
.create(RemoteApi::class.java)
}
您应该在其中用Api类替换RemoteApi。