前に読んだとおり、daggerはすべてのコンストラクタを生成して提供するため、事業内にコンストラクタはほとんど存在しません。Fragment
の中にRecyclerView
とActivity
は、私が働いているサンプルはhereあるAndroid、Dagger 2はフラグメント内にrecyclerviewを挿入します
LayoutManager
と
Adapter
を注入しても
Presenter
フラグメントのために(活動の内側にそれらを注入することなく、フラグメントに引数を通過する)ことができますどのように、フラグメントを注入されたが、彼は
Activity
を使用しましたそれ自体はパターンによると
View
として、私はを使用しようとしています代わりに10。
Thisは彼のActivity
です(リサイクラーのビューとプレゼンターはここに注入されます)。
マイコード:
UserComponent
@UserScope
@Subcomponent(modules = UserModule.class)
public interface UserComponent {
RepoListComponent plus(RepoListModule repoListModule);
UserEntity getUserEntity();
}
RepoListModule AppComponentのサブコンポーネントです:
@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
}
}
私はアダプタにアクセスできると言っていますか?私はそれをしたが動作していない – AlirezaXX
あなたはアダプタにアクセスすることができますか?あなたは自分でアダプタを作成します... – Fred
完全な答えをありがとうが、 'RepositoriesListComponent'はAppComponentのサブコンポーネントであるUserComponentのサブコンポーネントですので、アダプタとlayoutManagerがnullになりました! - >アクティビティ:=> 'GApplication.get(getApplicationContext()) .getUserComponent()。plus(RepoListModule(fragment)) .inject(フラグメント);' – AlirezaXX