我正在使用我的应用中有4个标签的View Pager。每个标签都会访问API并加载数据。
我正在使用setOffscreenPageLimit(4)
以防止每次刷选标签并使其可见时重新加载数据。这一切都有效。
然而,当应用程序打开时,它们的问题是最初触发所有4个选项卡。我所追求的行为只有在点击后才会加载给定的标签数据。
以下基本上是我的Tab Adapter的样子:
public class TabsFragmentPageAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 4;
private String tabTitles[] = new String[]{"f1", "f2", "f3", "f4"};
Fragment1 f1;
Fragment2 f2;
Fragment3 f3;
Fragment4 f4;
public TabsFragmentPageAdapter(FragmentManager fm) {
super(fm);
f1 = Fragment1.getInstance();
f2 = Fragment2.getInstance();
f3 = Fragment3.getInstance();
f4 = Fragment4.getInstance();
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public Fragment getItem(int position) {
if(position == 0)
{
return f1;
}
else if(position == 1)
{
return f2;
}
else if(position == 2)
{
return f3;
}
else if(position == 3)
{
return f4;
}
return null;
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
我能做些什么才能达到我想要的行为?
修改
基本片段列表示例:
public class Fragment1 extends Fragment {
public static Fragment1 getInstance() {
return new Fragment1();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list, container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView = (ListView) view.findViewById(R.id.list);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// get data and load list/adapter
}
}