片段类中的ViewPager无法正常工作

时间:2014-03-28 10:48:03

标签: android android-fragments android-viewpager android-fragmentactivity android-nested-fragment

我在Fragment类中使用了ViewPager。在那个ViewPager中,每个页面都有三个页面显示自定义列表视图.ViewPager有时会在一段时间内正确显示列表视图,显示空白页面。

使用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;
}
}

自定义列表视图类(我的所有三个自定义列表类,如下面的类)

 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;
}
}

我无法找到我的问题。任何人都知道帮助我解决这个问题。

1 个答案:

答案 0 :(得分:10)

在线路上实例化适配器时使用getChildFragmentManager()而不是super.getActivity().getSupportFragmentManager()

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