getItem() in FragmentStatePagerAdapter called twice

2.8k views Asked by At

I have a problem on getItem() method. I had read some of the comment and answer but could not understand on how to implement.

Here is my code :

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;
        }
    }

}

Here is the Fragment called to use the adapter :

@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;
}

Please assist me....Great thanks in advance.

1

There are 1 answers

0
Ryno C. On

getItem(..) will always be called twice because it loads the current View that the user see and it always loads the next view which the user cannot see yet. This allows for smoother transitions between screens(views).

You can set the number of screens that the adapter should load by calling:

mViewpager.setOffscreenPageLimit(5);

where 5 is the number of screens to load in advance. This means that getItem will now be called five times.

The minimum number of OffScreenPages are 1, it cannot be set to 0, thus getItem(..) will always execute at least twice.