为什么在替换Fragment后,ViewPager在此片段中不会创建其子片段

时间:2013-10-30 12:15:05

标签: android android-fragments android-viewpager

我的应用程序的结构是:

MainActivity有5个标签(ImageView + TextView),主要部分是FrameLayout,可以切换5 Fragment s。

在第一个Fragment(名为ScheduleFragment)中,它的功能是查询学校时间表(意味着有7页显示整周的时间表)。所以我使用ViewPager来显示时间表。

ViewPager正在使用FragmentPagerAdapter

首先,我启动我的应用程序,并输入一些内容来查询日程安排,onClick ---> Thread.start()---> sendMessage(Message)---> handler.handleMessage()---> updateUI()。

我的ViewPager显示结果时间表成功。

当我点击标签切换片段(ScheduleFragment更改为OtherFragment),然后切换回(ScheduleFragment)。ScheduleFragment中的ViewPager没有显示数据。

我打印一些生命周期方法来检查发生了什么。

当我获得计划数据成功时,我切换ViewPager页面。 ViewPager的每个片段页面都将调用onCreate()onCreateView()

当我单击制表符以切换其他MAIN片段并返回ScheduleFragment时。调用ScheduleFragment的onCreate()onCreateView(),但ViewPager的每个片段页面都没有发生。

ViewPager中没有显示任何内容,但当我切换2nd ViewPager的片段时,Logcat会打印第3个片段的onCreate()onCreateView()

我不知道如何解决这个问题。

//this method in ScheduleFragment.java  
private void updateSchedule(WeekSchedule ws) {
    Log.i(TAG, "updateSchedule - ws" + ws.toString());

    this.scheduleHeader.setVisibility(View.GONE);
    this.scheduleViewPager.setVisibility(View.VISIBLE);

    SchedulePagerAdapter schedulePagerAdapter = new SchedulePagerAdapter(getActivity().getSupportFragmentManager(), ws);
    //schedulePagerAdapter.notifyDataSetChanged();

    this.scheduleViewPager.setAdapter(schedulePagerAdapter);
}

这是OneDayScheduleFragment.java

//this is fragment in ViewPager, it for each day of week schedule.
public class OneDayScheduleFragment extends Fragment {

private final String TAG = "OneDayScheduleFragment";

private WeekSchedule ws;
private int weekDay;

public WeekSchedule getWs() {
    return ws;
}

public void setWs(WeekSchedule ws) {
    this.ws = ws;
}

public int getWeekDay() {
    return weekDay;
}

public void setWeekDay(int weekDay) {
    this.weekDay = weekDay;
}

public static OneDayScheduleFragment newInstance(int index, WeekSchedule ws) {
    OneDayScheduleFragment f = new OneDayScheduleFragment();
    Bundle b = new Bundle();
    b.putSerializable("weekSchedule", ws);
    b.putInt("weekDay", index);
    f.setArguments(b);
    return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG, "onCreate - " + weekDay);
}

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

    Log.i(TAG, "onCreateView - " + weekDay);

    View root = inflater.inflate(R.layout.fragment_weekschedule, null);
    ListView listView = (ListView)root.findViewById(R.id.schedule_result_listview);
    TextView textView = (TextView)root.findViewById(R.id.schedule_result_textview);

    int index = this.weekDay;
    WeekSchedule ws = this.ws;

    TodaySchedule ts = ws.todayScheduleArray.get(index - 1);
    int courseCount = ts.courseList.size();

    if (courseCount == 0) {
        //set header
        ...

    } else {
        //set header
        ...

        ScheduleListViewAdapter scheduleListViewAdapter = new ScheduleListViewAdapter(getActivity(), ws.todayScheduleArray.get(index - 1));
        scheduleListViewAdapter.notifyDataSetChanged();
        listView.setAdapter(scheduleListViewAdapter);
    }

    return root;
}
}

0 个答案:

没有答案