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

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

这是我的资料来源。

    @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 is replaced从后堆栈中 pop ,如果再次单击该用户,则为ojit_code的新实例
他们怎么了?

最佳答案

代替 :

HotelDetailPagerAdapter adapter = new HotelDetailPagerAdapter(getFragmentManager());

尝试:
HotelDetailPagerAdapter adapter = new HotelDetailPagerAdapter(getChildFragmentManager());



代替 :
FragmentTransaction ft = getBaseActivity().getFragmentManager().beginTransaction();

尝试:
FragmentTransaction ft = getChildFragmentManager().beginTransaction();

07-28 02:14
查看更多