0

I am using the actionbar tabs in my android application and for some reason the fragments that I want to load after the third tab don't load at all after the first time I change to them regardless of wheather I swipe to them or click on the tab, same result.

It is not a problem specific to the fragments that are being loaded I know this because if I flip flop them around in my fragmentstatepageradapter the same thing happens no matter which fragments I set to load on which tab it's always all the tabs after the third tab regardless of what fragment is connected to the third tab, I even made the third tab basically a blank fragment with a button and still nothing. Also always on the first tab change after the program starts up it takes a ridiculously long time to do the change but the tabs that are having problems usually always load on the first tab change after the program has started but then don't ever again. An interesting thing to note , I can still click on the list fragment list items and the action that happens when I do that still works but the items aren't visisble. Also noticed if I swipe from the furthest tab to the right(the non working ones) the 3rd tab will load but if I swipe from the furthest tab to the left to the 3rd tab it won't load.

here is my page change listener

 vpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
    {
        // This method will be invoked when a new page becomes selected.
        @Override
        public void onPageSelected(int position)
        {

                getActionBar().setSelectedNavigationItem(position);
                getActionBar().getTabAt(position).select();
        }

        // This method will be invoked when the current page is scrolled
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
        {
            // Code goes here
        }

        // Called when the scroll state changes:
        // SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
        @Override
        public void onPageScrollStateChanged(int state)
        {
            // Code goes here
        }
    });

here is my FragmentStatePagerAdapter

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;

import java.util.List;

public class AppSectionsPagerAdapter extends FragmentStatePagerAdapter
{
private static String[] fragmentNames = { "Example0", "Example1", "Example2","Example3","Example4"};
private FragmentManager fragmentManager = null;
private List<Fragment> fragments=null;
Example0 example0;
Example1 example1;
Example2 example2;
Example3 example3;
Example4 example4;
int numFragments;
int errorcode;
public AppSectionsPagerAdapter(FragmentManager fm)
{
    super(fm);
    numFragments = 5;
    this.fragmentManager = fm;
    example0 = new Example0();
    example1 = new Example1();
    example2 = new Example2();
    example3 = new Example3();
    example4 = new Example4();

}
@Override
public Fragment getItem(int i)
{
    switch (i)
    {
        case 0:
             return example0;
        case 1:
            return example1;
        case 2:
           return example2;
        case 3:
            return example3;
        case 4:
            return example4;

        default:
            Fragment frag = new Error_Fragment();
            Bundle args = new Bundle();
            errorcode = -5;
            //args.putInt(errorcode, i + 1);
            frag.setArguments(args);
            return frag;
    }
}
@Override
public int getCount()
{
   return numFragments;
}

@Override
public CharSequence getPageTitle(int position)
{
    return fragmentNames[position];
}

@Override
public void setPrimaryItem(ViewGroup container, int position, Object object)
{
    super.setPrimaryItem(container,0,object);
}
@Override
public void notifyDataSetChanged()
{
    super.notifyDataSetChanged();
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view)
{
    fragmentManager.executePendingTransactions();
    //fragmentManager.saveFragmentInstanceState(fragments.get(position));
}
}

and finally here is my main activity code

public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener
{

 @Override
 public void onCreate(Bundle savedInstanceState)
 {
    super.onCreate(savedInstanceState);
     AppSectionsPagerAdapter mAppSectionsPagerAdapter;
     setContentView(R.layout.activity_main);

      mAppSectionsPagerAdapter = new  AppSectionsPagerAdapter(getSupportFragmentManager());

    // Set up the action bar.
     ActionBar actionBar = getActionBar();

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    //actionBar.setCustomView(R.layout.tab_layout);

    // Set up the ViewPager, attaching the adapter for when the
    // user swipes between sections.
    vpager = (ViewPager) findViewById(R.id.pager);
    vpager.setAdapter(mAppSectionsPagerAdapter);


 for (int i = 0; i < 5; i++)
     {
        actionBar.addTab(
                actionBar.newTab()
                        .setIcon(tabResIds[i])
                        .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));

    }
    }

    }

2 Answers2

0

solved it by removing these lines from my fragment activity

        vpager.setPageTransformer(false, new ViewPager.PageTransformer() // page swipe animations
    {
        @Override
        public void transformPage(View page, float position)
        {
            int pageWidth = page.getWidth();
            int pageHeight = page.getHeight();

            if (position < -1)   // [-Infinity,-1)
            {
                // This page is way off-screen to the left.
                page.setAlpha(1);
            }
            else if(position <= 1)    // Page to the left, page centered, page to the right
            {
                // modify page view animations here for pages in view
            }
            else     // (1,+Infinity]
            {
                // This page is way off-screen to the right.
                page.setAlpha(0);
            }
        }
    });

it's setting the view pager's transformer animation and it messes with things.

Bob Saget
  • 67
  • 2
  • 8
0

fixed it by removing my viewpager's page transformer that I had set in my main FragmentActivity class

        vpager.setPageTransformer(false, new ViewPager.PageTransformer() // page swipe animations
    {
        @Override
        public void transformPage(View page, float position)
        {
            int pageWidth = page.getWidth();
            int pageHeight = page.getHeight();

            if (position < -1)   // [-Infinity,-1)
            {
                // This page is way off-screen to the left.
                page.setAlpha(1);
            }
            else if(position <= 1)    // Page to the left, page centered, page to the right
            {
                // modify page view animations here for pages in view
            }
            else     // (1,+Infinity]
            {
                // This page is way off-screen to the right.
                page.setAlpha(0);
            }
        }
    });

removed these lines

Bob Saget
  • 67
  • 2
  • 8