0

I have a viewpager with 5 tabs and if I go to the 5th fragment(that is the fragment loaded when I click the 5th tab or swipe to the 5th tab) and then I go to the 3rd tab the 5th fragment won't load the view anymore and if I go to the 2nd or 1st tab the 4th and 5th tabs won't. I tried changing the viewpager.offscreenlimit(3) or even 5 with no results. If it helps anyone figure it out, it happens regardless of what fragments I put in place of the 3rd 4th and 5th positions. Also, the first viewpager change takes a while , about 1.5 seconds.

here is my code

mport 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;
Fragment example0;
Fragment example1;
Fragment example2;
Fragment example3;
Fragment example4;
int numFragments;
int errorcode;

public AppSectionsPagerAdapter(FragmentManager fm)
{
    super(fm);
    numFragments = 5;
    this.fragmentManager = fm;
    example0 = new ListFragment();
    example1 = new ListFragment();
    example2 = new ListFragment();
    example3 = new Fragment();
    example4 = new Fragment();

}
@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));
}
}
Bob Saget
  • 67
  • 2
  • 8
  • I would start by getting rid of `destroyItem()`, `notifyDataSetChanged()`, and `setPrimaryItem()`. Then, I would change the superclass to `FragmentPagerAdapter`, as for only five pages, you do not need `FragmentStatePagerAdapter`. Also, you will have better performance if you create the fragments on demand in `getItem()`, rather than creating them up front. If none of that helps, you would need to explain in greater detail what "won't load the view anymore" means. – CommonsWare Jan 31 '15 at 21:58
  • Thank you for posting. I did try all your recommendations but still nothing. I'm starting to suspect it might be a memory issue so I'm putting the onlowmemory callbacks in and am going to disable showing of these images in my listviews because I got the android studio sample code to work with basic string listviews and I'm guessing it's these images that using a lot of memory. I will let you know – Bob Saget Jan 31 '15 at 22:51
  • Normally, you get more concrete problems when you run out of memory, in the form of `OutOfMemoryError` crashes. I do not know where these images are coming from, but if they are coming from the disk or the network, I strongly recommend the use of an image-loading library, such as Picasso or Universal Image Loader. – CommonsWare Jan 31 '15 at 22:53
  • for now they are just in the drawables but I definitely want to eventually do it over the network I will totally check out picasso and uil, if not write my own if needed. – Bob Saget Jan 31 '15 at 23:01
  • I stopped all images from loading but still have the problem. also set every switch case to return the same fragment and still have the problem. so that rules out a specific fragment causing the problem, or the pager adapter because I'm using the same pager adapter as the android studio sample program. – Bob Saget Jan 31 '15 at 23:14
  • Thanks for keeping me going CommonsWare I was about to give up but you made me re think things with your comment on the other page. I wouldn't ever of guessed it was the pagetransformer thingy especially since it barely does anything. – Bob Saget Feb 01 '15 at 00:00

1 Answers1

0

I finally figured it out! I had these lines of code

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

and I simply commented them out. I guess I should have posted that but I had no idea it could interfere with pages not loading.

Bob Saget
  • 67
  • 2
  • 8