当我刷另一个已经加载的片段时,再次加载该片段。如何解决?
我有Tab Pager的默认MainActivity:
public class MainActivity extends FragmentActivity {
ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
Tab = (ViewPager)findViewById(R.id.pager);
Tab.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar = getActionBar();
actionBar.setSelectedNavigationItem(position); }
});
Tab.setAdapter(TabAdapter);
actionBar = getActionBar();
//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener(){
@Override
public void onTabReselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
Tab.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(android.app.ActionBar.Tab tab,
FragmentTransaction ft) {
// TODO Auto-generated method stub
}};
//Add New Tab
actionBar.addTab(actionBar.newTab().setText("Все").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Цитаты").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Картинки").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("Видео").setTabListener(tabListener));
}
}
这并不酷,我有很多不必要的异步任务因为应用程序才关闭。
答案 0 :(得分:2)
您是否尝试过使用ViewPager的方法setOffscreenPageLimit()?