gpt4 book ai didi

android - 注入(inject)演示者子类以查看android

转载 作者:行者123 更新时间:2023-11-30 00:58:28 25 4
gpt4 key购买 nike

我正在使用 MVP 模式创建一个 android 应用程序。对于依赖项注入(inject),我使用 dagger 2。我有一个实现“ View ”接口(interface)的抽象 fragment ,使其成为 Model-View-Presenter 中的 View 。

public abstract class MediaDetailFragment extends Fragment implements DetailsMvpView {
private static final String RESOURCE = "resource";
private static final String POSTER_SIZE = "w342/";


@Inject DetailPresenter mDetailPresenter;
@Inject CreditPresenter mCreditPresenter;
@BindView(R.id.media_image_flipper) ControllableFlipper mImageFlipper;
@BindView(R.id.keyword_recyclerview) RecyclerView mKeywordRecyclerView;
@BindView(R.id.title_textview) TextView mTitleTextView;
@BindView(R.id.overview_content_textview) TextView mOverviewTextView;
@BindView(R.id.cast_recyclerview) CreditRecyclerView mCastRecyclerView;
@BindView(R.id.crew_recyclerview) CreditRecyclerView mCrewRecyclerView;
@BindView(R.id.empty_view) TextView mRecyclerviewEmpty;
@BindView(R.id.button_share) ImageButton mShare;
@BindView(R.id.button_trailers) ImageButton mTrailers;
@BindView(R.id.button_reviews) ImageButton mReviews;
@BindBool(R.bool.isTablet) boolean mIsTablet;

@OnClick(R.id.button_share) public void shareMedia(View view) {
startActivity(new Intent(Intent.ACTION_SEND)
.putExtra(Intent.EXTRA_TEXT, getActivity().getString(R.string.base_youtube_url)
+ POSTER_SIZE + mMedia.posterPath() + "\n\n"
+ mMedia.title() + "\n\n" + mMedia.overview())
.setType("text/plain"));
}

@OnClick(R.id.button_trailers) public void requestTrailers(View view) {
mDetailPresenter.loadMovies(mMedia.id());
}

@OnClick(R.id.button_reviews) public void requestReviews(View view) {
if (NetworkUtil.isNetworkConnected(getContext())) {
((FragmentHandler) getActivity()).onReviewsRequested(mMedia);
} else {
ViewUtil.displayNoNetworkSnackbar(getActivity());
}
}

private Media mMedia;
private DisplayMetrics mDisplayMetrics = new DisplayMetrics();
private OnGlobalLayoutListener mListener;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMedia = getArguments().getParcelable(RESOURCE);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.media_details_fragment, container, false);

((BaseActivity) getActivity()).activityComponent().inject(this);
ButterKnife.bind(this, rootView);

getActivity().getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);

mTitleTextView.setText(mMedia.title());
mOverviewTextView.setText(mMedia.overview());

mKeywordRecyclerView.setAdapter(new KeywordAdapter(getContext()));
mCastRecyclerView.setAdapter(R.layout.item_credit_normal, mRecyclerviewEmpty);
mCrewRecyclerView.setAdapter(R.layout.item_credit_normal, mRecyclerviewEmpty);

mListener = () -> rootView.post(() -> {
if (mIsTablet) {
mImageFlipper.getLayoutParams().height =
ViewUtil.setHeightForAspectRatio(rootView.getWidth(), ViewUtil.STANDARD);
} else {
mImageFlipper.getLayoutParams().height =
ViewUtil.setHeightForAspectRatio(mDisplayMetrics.widthPixels, ViewUtil.STANDARD);
}
rootView.getViewTreeObserver().removeOnGlobalLayoutListener(mListener);
});
rootView.getViewTreeObserver().addOnGlobalLayoutListener(mListener);

return rootView;
}

@Override
public void onStart() {
super.onStart();

mDetailPresenter.attachView(this);
mDetailPresenter.loadImages(mMedia.id());
mDetailPresenter.loadKeywords(mMedia.id());

mCreditPresenter.attachCastView(mCastRecyclerView);
mCreditPresenter.attachCrewView(mCrewRecyclerView);
mCreditPresenter.loadCredits(mMedia.id());
}

@Override
public void onStop() {
mDetailPresenter.detachView();
mCreditPresenter.detachCastView();
mCreditPresenter.detachCrewView();
super.onStop();
}

@Override
public void showImages(String image) {
mImageFlipper.addImagePath(image);
}

@Override
public void showKeywords(List<String> keywords) {
((KeywordAdapter) mKeywordRecyclerView.getAdapter()).setKeywords(keywords);
}

@Override
public void showVideos(List<Video> videos) {
TrailerDialogFragment.newInstance(videos).show(getFragmentManager(), null);
}

@Override
public void showError() {
if (!NetworkUtil.isNetworkConnected(getContext())) {
ViewUtil.displayNoNetworkSnackbar(getActivity());
}
}
}

根据情况,我将使用这个抽象 View 的两个子类之一。如您所见,我将“DetailsPresenter”注入(inject)到 View 中。 DetailsPresenter 也是一个具有两个子类的抽象类( View 的每个子类一个)。

但是,如果 View 是 MovieDetailFragment,它应该有一个 MovieDetailPresenter,如果它是 ShowDetailFragment,它应该有一个 ShowDetailPresenter。

我的问题是:我应该在哪里实现检查 View 类型并提供正确的 DetailsPresenter 的逻辑?我应该在 Dagger 模块中执行此操作(也许在返回演示者的方法中)吗?我应该在 fragment 本身中进行吗?

这是我的模块:

@Module
public class ConfigPersistentModule {

@Provides
DetailPresenter provideDetailPresenter(DataManager dataManager) {
return new MovieDetailPresenter(dataManager);
}
}

请帮帮我。

最佳答案

您必须在子类(MovieDetailFragment 和 ShowDetailFragment)中注入(inject)您的演示者,并且在您的模块中您需要有两个提供模块,如下所示:

@Module
public class ConfigPersistentModule {

@Provides
MovieDetailFragment provideDetailPresenter(DataManager dataManager) {
return new MovieDetailPresenter(dataManager);
}

@Provides
ShowDetailFragment provideDetailPresenter(DataManager dataManager) {
return new ShowDetailFragment(dataManager);
}
}

然后在你的子类中像这样注入(inject):

在 ShowDetailFragment 中:

@Inject ShowDetailFragment mCreditPresenter;

在 MovieDetailFragment 中:

@Inject MovieDetailFragment mCreditPresenter;

现在在您的父类 (MediaDetailFragment) 中放置一个名为“getPresenter”的抽象函数,子类必须覆盖它,如下所示:

public abstract class MediaDetailFragment extends Fragment implements DetailsMvpView {
private mDetailPresenter;

public abstract DetailPresenter getPresenter();

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
this.mDetailPresenter = getPresenter();
...
...
}
...
...
}

现在每个实现 MediaDetailFragment 的类都必须告诉父亲有关他的演示者的信息,而父亲则使用该演示者。所以在子类(MovieDetailFragment & ShowDetailFragment)中写:

@Override
public DetailPresenter getPresenter()
{
return mCreditPresenter;
}

关于android - 注入(inject)演示者子类以查看android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39801400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com