0

我的应用程序的结构是:

AMainActivity有 5 个选项卡(ImageView+ TextView),主要部分是FrameLayout开关 5Fragment秒。

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

ViewPager正在使用FragmentPagerAdapter.

首先,我启动我的应用程序,并输入一些查询计划,onClick ---> Thread.start() ---> sendMessage(Message) ---> handler.handleMessage() ---> updateUI() .

ViewPager的显示结果计划成功。

当我单击选项卡以切换 Fragment(ScheduleFragment更改为OtherFragment)并切换回(返回到ScheduleFragment)时。ViewPager 中的ScheduleFragment不显示数据。

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

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

当我单击选项卡切换其他主要片段并返回时ScheduleFragment。ScheduleFragment 的onCreate()并被onCreateView()调用,但 ViewPager 的每个片段页面上都没有发生任何事情。

什么都没有显示ViewPager,但是当我切换2ndViewPager 的片段时,Logcat打印第三个片段的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;
}
}
4

0 回答 0