2016-06-13 19 views
0

Ich habe eine Ansicht Pager, es funktioniert gut, wenn ich Fragment mit ihm zum ersten Mal öffnen. Nach dem Zurückgeben dieses Fragments aus dem Backstack habe ich einen leeren Bildschirm. Ich benutze ChildFragmentManager für FragmentStatePagerAdapter, aber es funktioniert immer noch nicht. Ich habe eine temporäre Lösung gefunden, indem ich jedes Mal einen neuen Adapter in onResume gesetzt habe, aber mir scheint, dass diese Lösung nicht der beste Weg ist, um dieses Problem zu lösen. Bitte, bitte hilf mir und sag mir, was ich falsch mache. Hier ist mein Code:ViewPager ist leer nach der Rückkehr von Backstak sogar mit der Verwendung von ChildFragmentManager und FragmentStatePagerAdapter

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); 
      } 
     }); 
    } 
} 

Antwort

1

es Sie Adapter scheint wieder anbringen nicht beim Neustart

if (mViewPagerAdapter == null) { 
    mViewPagerAdapter = new RecentReviewsViewPagerAdapter(getChildFragmentManager(), reviews); 
    mRecentReviewsViewPager.setAdapter(mViewPagerAdapter); // <<<< 
    mReviews = reviews; 
    return; 
} 
mReviews = reviews; 
mViewPagerAdapter.setData(reviews); 

diese Zeile aufgerufen werden muss, wenn mRecentReviewsViewPager keinen Adapter hat

+0

Ja, du hast absolut recht! Vielen Dank! –