简体   繁体   中英

ViewPager in fragment Class is not working properly

Am using ViewPager inside the Fragment class.In that ViewPager have three pages in each pages am show customized list view.ViewPager Some time shows the list view properly in some time it shows blank page.

Fragment class with ViewPager

public class Schedule extends Fragment{

RelativeLayout schedule_lay;
Schedule_fragment_adapter sfadp;
ViewPager pager;
TitlePageIndicator tpi;
PagerTabStrip pts;

@Override
public View onCreateView(LayoutInflater inflater,final ViewGroup container, Bundle savedInstanceState) {
    if (container == null) {
        return null;
    }

    schedule_lay = (RelativeLayout) inflater.inflate(R.layout.schedule, container, false);
    pager = (ViewPager) schedule_lay.findViewById(R.id.viewpager);
    pts = (PagerTabStrip)schedule_lay.findViewById(R.id.page_title_indicator);
    pts.setDrawFullUnderline(false);

    pts.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
    pts.setTabIndicatorColor(Color.parseColor("#ff3824"));

    initialisePaging();

    return schedule_lay;
}

private void initialisePaging() {

    List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(getActivity(), Ongoing.class.getName()));
    fragments.add(Fragment.instantiate(getActivity(), Upcoming.class.getName()));
    fragments.add(Fragment.instantiate(getActivity(), Recent.class.getName()));
    this.sfadp  = new Schedule_fragment_adapter(super.getActivity().getSupportFragmentManager(), fragments);

    pager.setAdapter(this.sfadp);
}
}

FragmentPagerAdapter

public class Schedule_fragment_adapter extends FragmentPagerAdapter {

private final List<Fragment> fragments;

/**
 * @param fm
 * @param fragments
 */
public Schedule_fragment_adapter(FragmentManager fm,
        List<Fragment> fragments) {
    super(fm);
    this.fragments = fragments;
}

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

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

@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
    case 0:
        return "Ongoing";
    case 1:
        return "Upcoming";
    case 2:
        return "Recent";
    }
    return null;
}
}

Customized list view Class (My all three customized list class like below class )

 public class Ongoing extends Fragment{

RelativeLayout ongoing_lay;
ListView ongoing_matches_lv;
ArrayList<Item> ongoing_matches = new ArrayList<Item>();

@Override
public View onCreateView(LayoutInflater inflater,final ViewGroup container, Bundle savedInstanceState) {
    if (container == null) {
        return null;
    }

    ongoing_lay = (RelativeLayout) inflater.inflate(R.layout.ongoing, container, false);


    return ongoing_lay;
}
}

Am not able find out my issue.Can any one know help me to solve this issue.

super.getActivity().getSupportFragmentManager()实例化适配器时,请使用getChildFragmentManager()而不是super.getActivity().getSupportFragmentManager()

this.sfadp  = new Schedule_fragment_adapter(super.getActivity().getSupportFragmentManager(), fragments);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM