来自片段的viewpager返回空白

时间:2017-03-08 12:44:08

标签: android android-layout android-studio android-fragments android-viewpager

我正在开发一款包含viewpagerTablayout的Android应用。这是我的查看分页器片段,它从父活动

打开
public class HomeFragment extends Fragment{
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;
public HomeActivity() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_home, container, false);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());

    mViewPager = (ViewPager) rootView.findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
    return rootView;
}
}

这是我的viewpager适配器

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {
    switch (position){
        case 0 : return new Settingsfragment();
        case 1 : return new Profilefragment();
        case 2 : return new Categoriesfragment();
    }
    return null;
}
@Override
public int getCount() {
    // Show 3 total pages.
    return 3;
}
@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
        case 0:
            return "settings";
        case 1:
            return "profile";
        case 2:
            return "Category";
    }
    return null;
}
}

当我用下面的代码打开一个片段并按回viewpager变为空

public class Profilefragment extends Fragment {
 public Profilefragment(){}
 @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.profile_layout, container, false);

    TextView VE = (TextView) rootView.findViewById(R.id.profile_view_enquiry);
    VE.setOnClickListener(new View.OnClickListener(){
        public void onClick (View view){
            Fragment fragment = null;
            fragment = new Enquiryfragment();
            replaceFragment(fragment);
        }
    });

    TextView CA = (TextView) rootView.findViewById(R.id.profile_change_address);
    CA.setOnClickListener(new View.OnClickListener(){
        public void onClick (View view){
            Fragment fragment = null;
            fragment = new ChangeAddressfragment();
            replaceFragment(fragment);
        }
    });

    return rootView;
}
public void replaceFragment(Fragment someFragment) {
    android.support.v4.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.containerView, someFragment);
    transaction.addToBackStack(null);
    transaction.commit();
}}

我该如何解决这个问题。请帮助。

1 个答案:

答案 0 :(得分:1)

public class HomeFragment extends Fragment{
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;

    public HomeFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());//Changed here

        mViewPager = (ViewPager) rootView.findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
        return rootView;
    }
}

您应该在getChildFragmentManager()

中使用getFragmentManager()代替Fragment