FragmentPagerAdapter getItem()返回的内容超出预期

时间:2015-08-03 15:17:48

标签: android android-fragments tabs android-tabs

我有一个MainActivity来控制四个片段,每个片段都是一个标签。当我的主要活动开始时,我会在日志中打印一行,以显示正在实例化的片段。这是我的FragmentPagerAdapter:

public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {
        case 0:
            System.out.println("Returning new PrearrivalPlan()");
            return new PrearrivalPlan();
        case 1:
            System.out.println("Returning new PrimarySurvey()");
            return new PrimarySurvey();
        case 2:
            System.out.println("Returning new SecondarySurvey()");
            return new SecondarySurvey();
        case 3:
            System.out.println("Returning new PrepareForTravel()");
            return new PrepareForTravel();
    }

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 4;
}

}

标签栏按顺序包含以下选项:

Prearrival Plan | Primary Survey | Secondary Survey | Prepare for Travel

当我的主要活动开始时,屏幕上会显示以下信息:

Returning new PrearrivalPlan() Returning new PrimarySurvey()

它似乎正在做的是在我选择的一个标签之前加载一个标签。由于PrearrivalPlan是第一个标签,我认为它应该只返回一个新的PrearrivalPlan(),除非它返回两者。另一个例子,当我点击Primary Survey选项卡(第二个标签)时,屏幕上会显示以下信息:

Returning new SecondarySurvey() // <- This is the third tab!?

因为在活动首次启动时已经实例化了PrimarySurvey(参见上面的输出),它就像之前一样向前跳跃并加载了第三个标签,即使我只点击了第二个标签。

这是我的MainActivity

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

private CustomViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;

private String[] tabTitles = {"Pre-arrival Plan", "Primary Survey", "Secondary Survey", "Prepare for Travel"};

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    viewPager = (CustomViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    viewPager.setPagingEnabled(false);

    actionBar.setHomeButtonEnabled(true);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


    viewPager.setOnPageChangeListener(new CustomViewPager.OnPageChangeListener() {


        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

    });

    // Remove Android icon from Action Bar
    getActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
    getActionBar().setDisplayShowHomeEnabled(false);

    // Add tabs to Action Bar
    for (String tab_name : tabTitles) {
        actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

public void updateTabTitles(int tabNumber, int checkBoxesRemaining) {
    String text = tabTitles[tabNumber] + " \n             (" + checkBoxesRemaining + ")";
    actionBar.getTabAt(tabNumber).setText(text);
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent intent = new Intent(this, MainMenu.class);
            startActivity(intent);
            return true;
        case R.id.complete:
            DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which){
                        case DialogInterface.BUTTON_POSITIVE:
                            goToReport();
                            break;
                        case DialogInterface.BUTTON_NEGATIVE:
                            //No button clicked
                            break;
                    }
                }
            };
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Are you sure you want to complete the checklist?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

public void goToReport() {
    Intent intent = new Intent(this, Report.class);
    startActivity(intent);
}

1 个答案:

答案 0 :(得分:2)

这是ViewPager的预期行为。 ViewPager始终在内存中保留一个前后标签,以显示滑块动画。如果你没有内存中的下一个或上一个标签(或片段),尝试在滑块转换期间启动片段将导致性能滞后或者更糟糕的是你将滑动片段变为空并且稍后加载。

ViewPager还允许您通过setOffscreenPageLimit()设置页面偏移限制,这将控制数量片段以将其保留在内存中。默认情况下,这是1,这意味着前面和后面的一个片段将始终存在于内存中