FragmentStatePagerAdapter中的getItem()调用两次

时间:2015-06-18 05:24:46

标签: android fragmentstatepageradapter

我在getItem()方法上遇到了问题。我已经阅读了一些评论和答案,但无法理解如何实施。

这是我的代码:

public Fragment getItem(int position) {
    currentItem = position;
    System.out.println("getItem : "+position);
    if(NumbOfTabs == 4){
        if(position == 0) // if the position is 0 we are returning the First tab
        {
            OpenJobTab tab = new OpenJobTab();
            return tab;
        }
        else if (position == 1)            // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
        {
            ClosedJobTab tab = new ClosedJobTab();
            return tab;
        }
        else if (position == 2)            // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
        {
            AssignedJobTab tab = new AssignedJobTab();
            return tab;
        }
        else            // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
        {
            ToDoJobTab tab = new ToDoJobTab();
            return tab;
        }
    }else{
        if(position == 0) // if the position is 0 we are returning the First tab
        {
            OpenJobTab tab = new OpenJobTab();
            return tab;
        }
        else if (position == 1)            // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
        {
            ClosedJobTab tab = new ClosedJobTab();
            return tab;
        }
        else            // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
        {
            ToDoJobTab tab = new ToDoJobTab();
            return tab;
        }
    }

}

这是要使用适配器的片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.tabbed_fragment, container, false);
    sharedpreferences = getActivity().getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
    isAdmin = sharedpreferences.getBoolean(getString(R.string.prefs_key_isAdmin), false);
    isTeam = sharedpreferences.getBoolean(getString(R.string.prefs_key_isTeam), false);

    TitlesAdmin = getResources().getStringArray(R.array.isAdminTabs);
    TitlesTeam = getResources().getStringArray(R.array.isTeamTabs);

    ws = new WebService(getActivity());

    // Inflate the layout for this fragment
    // Creating toolbar and set it as the Toolbar
    toolbar = (Toolbar) v.findViewById(R.id.tool_bar);
    toolbar.setOnMenuItemClickListener(new OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            // TODO Auto-generated method stub

            switch(item.getItemId()){
                case R.id.home:
                    Toast.makeText(getActivity(), "HOME", Toast.LENGTH_SHORT).show();
            }

            return true;
        }
    });

    ActionBarActivity activity = (ActionBarActivity)getActivity();
    activity.setSupportActionBar(toolbar);
    activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    activity.getSupportActionBar().setDisplayShowTitleEnabled(false);

    // Creating the viewpageradapter and passing fragment
    if((isAdmin) && (isTeam) || (isAdmin)){
        Numboftabs=4;
        adapter =  new TabbedAdapter(getActivity().getSupportFragmentManager(), TitlesAdmin, Numboftabs);
    }else{
        Numboftabs=3;
        adapter =  new TabbedAdapter(getActivity().getSupportFragmentManager(), TitlesTeam, Numboftabs);
    }

    // Assigning ViewPager view and setting the adapter
    pager = (ViewPager) v.findViewById(R.id.pager);
    pager.setAdapter(adapter);
    pager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            adapter.getItem(arg0);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }
    });
    // Assigning the Sliding Tab Layout View
    tabs = (SlidingTabLayout) v.findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true);

    // Setting custom color for the scroll bar indicator
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {

        @Override
        public int getIndicatorColor(int position) {
        // TODO Auto-generated method stub
            return getResources().getColor(R.color.tabsScrollColor);
        }

        @Override
        public int getDividerColor(int position) {
        // TODO Auto-generated method stub
            return 0;
        }
    });
        // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);
    setHasOptionsMenu(true);


  return v;
}

请帮助我......非常感谢提前。

1 个答案:

答案 0 :(得分:4)

getItem(..)将始终被调用两次,因为它加载了用户看到的当前视图,并且它总是加载用户还看不到的下一个视图。这样可以在屏幕(视图)之间实现更平滑的过渡。

您可以通过调用

来设置适配器应加载的屏幕数
mViewpager.setOffscreenPageLimit(5);

其中5是预先加载的屏幕数。这意味着现在将调用getItem五次。

OffScreenPages的最小数量为1,不能设置为0,因此getItem(..)将始终执行至少两次。