1
onCreateView
我在FragmentPagerAdapter中的第一個片段不是很快。 因此,將curent選項卡更改爲第一個會有延遲。如何禁用選項卡上的FragmentPagerAdapter中第一個片段的重新創建?
如何禁用在選項卡上的FragmentPagerAdapter中重新創建第一個片段?
private class TabsAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener, ActionBar.TabListener {
private final Context context;
private ActionBar bar;
private final ViewPager viewPager;
private final ArrayList<TabInfo> tabs = new ArrayList<TabInfo>();
final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
context = activity;
bar = activity.getSupportActionBar();
viewPager = pager;
viewPager.setAdapter(this);
viewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
tabs.add(info);
bar.addTab(tab);
notifyDataSetChanged();
}
@Override
public int getCount() {
return tabs.size();
}
@Override
public Fragment getItem(int position) {
TabInfo info = tabs.get(position);
Fragment fragment = Fragment.instantiate(context, info.clss.getName(), info.args);
return fragment;
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
@Override
public void onPageScrollStateChanged(int state) {}
@Override
public void onPageSelected(int position) {
bar.setSelectedNavigationItem(position);
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Object tag = tab.getTag();
for (int i = 0; i < tabs.size(); i++) {
if (tabs.get(i) == tag)
viewPager.setCurrentItem(i);
}
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {}
}
酷! 非常感謝! – Sviatoslav