我在Fragment中有TabLayout,然后使用FragmentTransaction从Main Activity到Fragment,但是当从Activity到Fragment的事务显示出这个问题时。
toolbar = (Toolbar) view.findViewById(R.id.Toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
// setSupportActionBar(toolbar);
// getSupportActionBar()。setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager)view. findViewById(R.id.viewpager1);
setupViewPager(viewPager);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
// setupTabIcons();
return view;
}
// Add Fragments to Tabs
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
// ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getFragmentManager());
adapter.addFrag(new AllFragment() ,"All");
adapter.addFrag(new ManFragment(),"Man");
adapter.addFrag(new WomenFragment(),"Women");
adapter.addFrag( new KidsFragment(),"Kids");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}