我有一个视图寻呼机,当我第一次用它打开片段时,它工作正常。从后台堆栈返回此片段后,我有一个空屏幕。我使用ChildFragmentManager for FragmentStatePagerAdapter但它仍然无法正常工作。通过每次在onResume中设置新适配器找到临时解决方案但在我看来这个解决方案不是解决此问题的最佳方法。请你帮我,告诉我我做错了什么。这是我的代码:
public class RecentReviewsCollapsedContainerFragment extends AbstractFragmentView
implements RecentReviewsCollapsedContract.View {
@BindView(R.id.recent_reviews_view_pager)
ViewPager mRecentReviewsViewPager;
@BindView(R.id.server_connection_button_try_again)
Button mTryAgainButton;
RecentReviewsViewPagerAdapter mViewPagerAdapter;
RecentReviewsCollapsedContract.Presenter mPresenter;
List<Review> mReviews;
public RecentReviewsCollapsedContainerFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_recent_reviews_collapsed_container, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
mPresenter = new RecentReviewsCollapsedPresenter(this);
mPresenter.loadReviews();
setUpViewPager();
}
@Override
public void onResume() {
super.onResume();
if ( mViewPagerAdapter != null ) {
cleanChildFragments(getChildFragmentManager());
mViewPagerAdapter = new RecentReviewsViewPagerAdapter(getChildFragmentManager(), mReviews);
mRecentReviewsViewPager.setAdapter(mViewPagerAdapter);
}
}
@Override
public void startProgress() {
showProgress();
}
@Override
public void stopProgress() {
hideProgress();
}
@Override
public void showConnectionErrorMessage() {
showServerConnectionError();
}
@Override
public void showNoItems() {
showNoItems();
}
@Override
public void setReviewsList(List<Review> reviews) {
if ( mViewPagerAdapter == null ) {
mViewPagerAdapter = new RecentReviewsViewPagerAdapter(getChildFragmentManager(), reviews);
mRecentReviewsViewPager.setAdapter(mViewPagerAdapter);
mReviews = reviews;
return;
}
mReviews = reviews;
mViewPagerAdapter.setData(reviews);
}
private void setUpViewPager() {
mRecentReviewsViewPager.setPageMargin(getResources()
.getDimensionPixelOffset(R.dimen.viewpager_margin));
}
private void cleanChildFragments(FragmentManager childFragmentManager) {
List<Fragment> childFragments = childFragmentManager.getFragments();
if (childFragments != null && !childFragments.isEmpty()) {
FragmentTransaction ft = childFragmentManager.beginTransaction();
for (Fragment fragment : childFragments) {
ft.remove(fragment);
}
ft.commit();
}
}
}
public class RecentReviewsViewPagerAdapter extends FragmentStatePagerAdapter {
private List<Review> mReviews;
public RecentReviewsViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public RecentReviewsViewPagerAdapter(FragmentManager fm, List<Review> reviews) {
super(fm);
mReviews = reviews;
}
@Override
public Fragment getItem(int position) {
RecentReviewCollapsedItemFragment reviewItem = new RecentReviewsViewPagerAdapter.RecentReviewCollapsedItemFragment();
reviewItem.setReview(mReviews.get(position));
return reviewItem;
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
@Override
public int getCount() {
return mReviews.size();
}
public void setData(List<Review> reviews) {
mReviews = reviews;
notifyDataSetChanged();
}
public static class RecentReviewCollapsedItemFragment extends Fragment {
private Review mReview;
@BindView(R.id.recent_reviews_item_scroll_view)
ObservableScrollView scrollView;
@BindView(R.id.recent_reviews_collapsed_item_text_view_date)
TextView mDateTextView;
@BindView(R.id.recent_reviews_collapsed_item_rating_bar_dealer)
RatingBar mDealerRating;
@BindView(R.id.recent_reviews_collapsed_item_rating_bar_user)
RatingBar mEmployeeRatingBar;
@BindView(R.id.recent_reviews_collapsed_item_text_view_title)
TextView mTitle;
@BindView(R.id.recent_reviews_collapsed_item_text_view_review_author_name)
TextView mAuthorNameTextView;
@BindView(R.id.recent_reviews_collpsed_item_image_view_verified)
ImageView mVerifiedCustomerImageView;
@BindView(R.id.recent_reviews_collapsed_item_text_view_verified_customer)
TextView mVerifiedCustomerTextView;
@BindView(R.id.recent_reviews_collapsed_item_text_view_review_content)
TextView mReviewContentTextView;
@BindView(R.id.recent_review_collapsed_item_ratingbar_customer_service)
RatingBar mCustomerServiceRatingBar;
@BindView(R.id.recent_review_collapsed_item_ratingbar_quality_of_work)
RatingBar mQualityOfWorkRatingBar;
@BindView(R.id.recent_review_collapsed_item_ratingbar_friendliness)
RatingBar mFriendlinessRatingBar;
@BindView(R.id.recent_review_collapsed_item_ratingbar_pricing)
RatingBar mPricingRatingBar;
@BindView(R.id.recent_review_collapsed_item_text_view_recommend_dealer_value)
TextView mRecommendDealerValueTextView;
public RecentReviewCollapsedItemFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_recent_review_collapsed_item, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
bindData();
setOnClickListener(scrollView);
setOnClickListener(mDateTextView);
}
@Override
public void onResume() {
super.onResume();
}
public void setReview(Review review) {
mReview = review;
}
private void bindData() {
mDateTextView.setText(mReview.getDate());
mDealerRating.setRating((int) mReview.getRating());
mEmployeeRatingBar.setRating((int) mReview.getEmployeeRating());
mTitle.setText(String.format("\"%s\"", mReview.getTitle()));
mAuthorNameTextView.setText(String.format("- %s", mReview.getReviewUserName()));
setVerifiedCustomer();
mReviewContentTextView.setText(mReview.getReviewBody());
mCustomerServiceRatingBar.setRating(mReview.getCustomerServiceRating());
mQualityOfWorkRatingBar.setRating(mReview.getQualityRating());
mFriendlinessRatingBar.setRating(mReview.getFriendlinessRating());
mPricingRatingBar.setRating(mReview.getPricingRating());
setRecommendDealer();
}
private void setVerifiedCustomer() {
if ( mReview.isVerifiedCustomer() ) {
mVerifiedCustomerTextView.setVisibility(View.VISIBLE);
mVerifiedCustomerImageView.setVisibility(View.VISIBLE);
return;
}
mVerifiedCustomerTextView.setVisibility(View.INVISIBLE);
mVerifiedCustomerImageView.setVisibility(View.INVISIBLE);
}
private void setRecommendDealer() {
if ( mReview.isRecommendDealer() ) {
mRecommendDealerValueTextView.setText(getActivity().getString(R.string.yes));
return;
}
mRecommendDealerValueTextView.setText(getActivity().getString(R.string.no));
}
private void setOnClickListener(View view) {
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FullReviewFragment fragment = new FullReviewFragment();
fragment.setReview(mReview);
((ViewContainer) getActivity()).showFragment(fragment, true);
}
});
}
}
答案 0 :(得分:1)
似乎你没有在重新启动时重新连接适配器
if ( mViewPagerAdapter == null ) {
mViewPagerAdapter = new RecentReviewsViewPagerAdapter(getChildFragmentManager(), reviews);
mRecentReviewsViewPager.setAdapter(mViewPagerAdapter); // <<<<
mReviews = reviews;
return;
}
mReviews = reviews;
mViewPagerAdapter.setData(reviews);
只要mRecentReviewsViewPager没有适配器,就必须调用此行