第一次加载后,屏幕外ViewPager片段将无法渲染 [英] offscreen viewpager fragments won't render after 1st time loaded
问题描述
我有一个带有5个选项卡的viewpager,如果我转到第5个片段(即单击第5个选项卡或滑动到第5个选项卡时加载的片段),然后转到第3个选项卡,第5个片段将不会不再加载视图,如果我转到第二个或第一个选项卡,则第四个和第五个选项卡将不会.我尝试更改viewpager.offscreenlimit(3)甚至5,但没有结果.如果它可以帮助任何人弄清楚,无论我取代第3位的第4位和第5位使用什么片段,它都会发生.另外,第一次对viewpager进行更改大约需要1.5秒的时间.
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.
这是我的代码
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));
}
}
推荐答案
我终于明白了!我有这些代码行
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.
这篇关于第一次加载后,屏幕外ViewPager片段将无法渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!