gpt4 book ai didi

android - 再次替换根Fragment时,Fragment的ViewPager不显示其内容

转载 作者:IT老高 更新时间:2023-10-28 23:02:44 27 4
gpt4 key购买 nike

我正在实现包含 root view pagerfragment screen,其中包含 child fragment 和第一个 child fragment 有一个显示图像的 view pager

第一次显示很好,但是当包含 root view pagerfragment 被删除并再次替换时(使用 fragment screen 的新关键字), roow view pager 不显示 child fragment

这是我的源代码的一部分。

    @AfterViews // this is the method of fragment screen
protected void afterViews() {
api(HotelDetailAPI.class).getProduct("hotels", getArguments().getInt("itemId"), new Callback<Hotel>(this) {
@Override
public void success(Hotel _hotel, Response response) {
hotel = _hotel;
HotelDetailPagerAdapter adapter = new HotelDetailPagerAdapter(getFragmentManager());
contentViewPager.setAdapter(adapter);
contentViewPager.setBackgroundColor(0xffffff);
super.success(hotel, response);
}

@Override
public void failure(RetrofitError error) {
super.failure(error);
LOG.error("failed");
}
});
}


private class HotelDetailPagerAdapter extends FragmentPagerAdapter {
List<Fragment> fragments = new ArrayList<>();

public HotelDetailPagerAdapter(FragmentManager fm) {
super(fm);
Bundle data = new Bundle();
data.putSerializable("hotel", hotel);
Fragment fragment1 = new HotelDetailReservationFragment_();
Fragment fragment2 = new HotelDetailReservationFragment_();
Fragment fragment3 = new HotelDetailReservationFragment_();
fragment1.setArguments(data);
fragment2.setArguments(data);
fragment3.setArguments(data);
fragments.add(fragment1);
fragments.add(fragment2);
fragments.add(fragment3);
}

@Override
public Fragment getItem(int position) {
return fragments.get(position);
}

@Override
public int getCount() {
return 3;
}
}


// this is the first fragment of root view pager it's not shown again.
@EFragment(R.layout.fragment_hotel_detail_reservation)
public class HotelDetailReservationFragment extends BaseFragment {
private Hotel hotel;

@AfterViews
public void afterViews() {
hotel = (Hotel)getArguments().getSerializable("hotel");
FragmentTransaction ft = getBaseActivity().getFragmentManager().beginTransaction();
Fragment fragment = new ImageSliderFragment_();
Bundle data = new Bundle();
data.putStringArray("images", hotel.getImages());
fragment.setArguments(data);
ft.add(R.id.hotel_detail_image_slider_container, fragment);
ft.commit();
}
}

注意fragment screen在用户按下返回键时从back stack中弹出,如果用户再次点击item,fragment screen的新实例被替换他们怎么了?

最佳答案

代替:

HotelDetailPagerAdapter adapter = new HotelDetailPagerAdapter(getFragmentManager());

试试:

HotelDetailPagerAdapter adapter = new HotelDetailPagerAdapter(getChildFragmentManager());

而不是:

FragmentTransaction ft = getBaseActivity().getFragmentManager().beginTransaction();

试试:

FragmentTransaction ft = getChildFragmentManager().beginTransaction();

关于android - 再次替换根Fragment时,Fragment的ViewPager不显示其内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25027931/

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