이전에 읽었던 것처럼 대거는 모든 생성자를 내 보내고 제공하므로 사업 내에서 거의 모든 생성자가 없어야합니다. 이제 Fragment
안에 RecyclerView
이 있고, Activity
내가 (활동 내부를 주입없이 조각에 인수 통과) LayoutManager
및 Adapter
과 조각에도 Presenter
를 주입 나는이 here을 일하고 있어요안드로이드, Dagger 2는 프래그먼트 안에 recyclerview를 삽입합니다.
샘플을 수있는 방법, 조각을 주입하지만, 그는 Activity
을 사용 자체는 패턴에 따라 View
이지만 사용하려고 시도하고 있습니다 Fragment
대신.
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 (조각)) .inject (조각);' – AlirezaXX