Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

android - offscreen viewpager fragments won't render after 1st time loaded

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));
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...