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

时光总嘲笑我的痴心妄想 提交于 2019-12-24 23:46:04

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/28252806/offscreen-viewpager-fragments-wont-render-after-1st-time-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!