我的导航抽屉有6个片段片段,其中一个片段包含2个其他片段,这些片段可用于viewPager。我的问题是,当我从包含viewPager(HomeFragment)的片段切换到另一个片段并切换回来时,HomeFragment中两个片段的内容都消失了。

这是我的代码

MainActivity.class

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ...
    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
            new HomeFragment()).commit();
    navigationView.setCheckedItem(R.id.nav_home);
    ...
}

 @Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {

        case R.id.nav_home:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new HomeFragment()).commit();
            toolbar.setTitle("mSeries");
            toolbar.setTitleTextColor(getResources().getColor(R.color.colorPrimaryDark));
            break;
    ...


HomeFragment.class

public class HomeFragment extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_home, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    ViewPager viewPager = view.findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    TabLayout tabLayout = view.findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

private void setupViewPager(ViewPager viewPager) {

    ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
    adapter.addFragment(new UpcomingShowsActivity(), "Upcoming episodes");
    adapter.addFragment(new UnwatchedEpisodesActivity(), "Unwatched episodes");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

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

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
  }
}


UpcomingShowsActivity.class

public class UpcomingShowsActivity extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_upcoming_episodes, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
 }
}


我是android的新手,如果有更好或更有效的方式来实现这一目标,请告诉我。

谢谢你的时间。

最佳答案

我自己修复了此问题,并在以后发布,以防将来有人遇到类似问题

因为我要在另一个片段中创建一个片段,所以应该在HomeFragment.class中使用getChildFragmentManager而不是get getFragmentManager

private void setupViewPager(ViewPager viewPager) {

    ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
    adapter.addFragment(new UpcomingShowsActivity(), "Upcoming episodes");
    adapter.addFragment(new UnwatchedEpisodesActivity(), "Unwatched episodes");
    viewPager.setAdapter(adapter);
}

10-07 12:44