【发布时间】:2016-08-18 01:30:47
【问题描述】:
我有三个片段,分别是 A、B 和 C,其中一个带有 viewpager,它包含多个片段(比如片段 B 有)。
虽然切换到fragment b会在移动到其他fragment并返回fragment b后渲染fragment viewpager内容,这里会重新加载内容。
我只想在片段渲染后停止销毁它??
提前致谢。
适配器示例代码:
public class QuestionsSortPagerAdapter extends FragmentPagerAdapter {
int mNumOfTabs;
public QuestionsSortPagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
QuestionsSortByVotes byVotes = new QuestionsSortByVotes();
return byVotes;
case 1:
QuestionsSortByActivity byActivity = new QuestionsSortByActivity();
return byActivity;
case 2:
QuestionsSortByHot byHot = new QuestionsSortByHot();
return byHot;
case 3:
QuestionsSortByDate byDate = new QuestionsSortByDate();
return byDate;
case 4:
QuestionsSortByMonth byMonth = new QuestionsSortByMonth();
return byMonth;
default:
return null;
}
【问题讨论】:
-
你使用什么 ViewPager 适配器?
-
我正在使用 FragmentPagerAdapter
-
FragmentPagerAdapter 将保留所有片段,它只是破坏用户不可见的片段视图。
-
但在我的情况下,当移动到这个视图页面之外的其他片段(使用导航抽屉)并来到这个包含 5 个以上片段的片段时。它再次重新加载。这里的 Fragments 将请求从服务器获取数据,我不想一次又一次地向服务器发出另一个请求。
标签: android android-fragments android-viewpager