Java Android - Tab Pager不重启片段

时间:2014-04-13 16:07:04

标签: java android android-fragments tabpage

当我刷另一个已经加载的片段时,再次加载该片段。如何解决?

我有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));
    }
}

这并不酷,我有很多不必要的异步任务因为应用程序才关闭。

1 个答案:

答案 0 :(得分:2)

您是否尝试过使用ViewPager的方法setOffscreenPageLimit()?

继承了开发者信息:http://developer.android.com/reference/android/support/v4/view/ViewPager.html#setOffscreenPageLimit(int)