Wie ich vorher gelesen habe, kommt Dolch alle Konstruktoren emittieren und stellen sie selbst, so sollten wir fast keinen Konstruktor in unserem Geschäft haben, jetzt vorstellen, gibt es eine RecyclerView
innerhalb einer Fragment
und die Activity
wird das Fragment Injektion, wie kann ich LayoutManager
und Adapter
und auch Presenter
für Fragment injizieren (ohne sie innerhalb Aktivität Injektion und durch Argumente zu Fragment passieren)Android, Dolch 2 inject recyclerview in Fragment
die Probe arbeite ich an ist here, aber er verwendet Activity
selbst als View
nach MVP
Muster, aber ich versuchezu verwenden 10 stattdessen.
This ist sein Activity
(dass Recycler-Ansicht und Presenter hier injiziert werden).
Mein Code:
UserComponent die Subkomponente AppComponent
@UserScope
@Subcomponent(modules = UserModule.class)
public interface UserComponent {
RepoListComponent plus(RepoListModule repoListModule);
UserEntity getUserEntity();
}
RepoListModule ist:
@Module
public class RepoListModule {
private RepoListContract.View view;
public RepoListModule(RepoListContract.View view) {
this.view = view;
}
@Provides
RepoListContract.View provideRepoListContractView(){
return view;
}
@Provides
LinearLayoutManager provideLayoutManager(Context context) {
return new LinearLayoutManager(context);
}
@Provides
RepoListAdapter provideAdapter(RepoListContract.View view) {
return new RepoListAdapter(view);
}
}
RepoListComponent:
@Subcomponent(modules = RepoListModule.class)
public interface RepoListComponent {
void inject(RepoListContract.View view);
}
RepoListActivity:
public class RepoListActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_repo_list);
RepoListFragment fragment = (RepoListFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = new RepoListFragment();
ActivityUtils.addFragmentToActivity(getSupportFragmentManager(),
fragment, R.id.fragment_container);
}
GApplication.get(getApplicationContext())
.getUserComponent().plus(new RepoListModule(fragment))
.inject(fragment);
}
}
RepoListFragment:
public class RepoListFragment extends Fragment implements RepoListContract.View {
@BindView(R.id.rv_repo) RecyclerView rvRepo;
@Inject RepoListContract.Presenter presenter;
@Inject LinearLayoutManager layoutManager;
@Inject RepoListAdapter adapter;
public static RepoListFragment newInstance() {
return new RepoListFragment();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_repo_list, container, false);
ButterKnife.bind(this, v);
initRvRepo();
return v;
}
private void initRvRepo() {
rvRepo.setLayoutManager(layoutManager); // null
rvRepo.setAdapter(adapter); //null
}
@Override
public void onResume() {
super.onResume();
presenter.subscribe(); //NullPointerException
}
}
Willst du sagen, dass ich zum Beispiel Adapter haben kann? Ich habe es aber nicht funktioniert – AlirezaXX
Was meinen Sie können Sie auf den Adapter zugreifen? Du erstellst den Adapter selbst ... – Fred
Danke für deine vollständige Antwort, aber da 'RepositoriesListComponent' Unterkomponente von UserComponent ist, die die Subkomponente von AppComponent ist, kann es passieren, jetzt sind mein Adapter und mein Layoutmanager null! -> Aktivität: => 'GApplication.get (getApplicationContext()) .getUserComponent(). Plus (neues RepoListModule (Fragment)) .inject (fragment);' – AlirezaXX