我将一个RecyclerView放在ViewPager中。当我打开我的应用程序时,它正常工作。当我转动屏幕时,它正常工作。如果我最小化应用程序,几分钟后我回到它一切正常。但是,当我将它留在后台很长一段时间并返回时,所有已加载的页面(三个)都是空的。我可以正常地与ViewPager交互,如果我滚到一边,我会进入显示内容的RecyclerView。如果我走得足够远,导致加载的列表在我回来时被销毁,那么它们是正常的。问题是应用程序返回的那一刻,并且已经加载的页面上没有任何内容。任何人都可以告诉我为什么会这样,我该如何解决?
所以,这里有一些代码:
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
db = new VirtualDataBase(this);
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
tabLayout.setupWithViewPager(mViewPager);
mViewPager.setCurrentItem(selectedTab);
}
然后片段:
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setRetainInstance(true);
fragment.setArguments(args);
fragment.setSectionId(sectionNumber);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
if(savedInstanceState != null){
rootView.invalidate();
}
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
meusItems= new MeusItems();
myDataset = new ArrayList<>();
db = new VirtualDataBase(getContext());
list1 = db.getList1();
if (sectionId == 1) {
mAdapter = new MainList1Adapter(list1, this);
mRecyclerView.setAdapter(mAdapter);
} else if (sectionId == 2) {
myDataset = meusItems.getFirstPart();
mAdapter = new MainAdapter(myDataset, this);
mRecyclerView.setAdapter(mAdapter);
} else if (sectionId == 3) {
myDataset = meusItems.getSecondPart();
mAdapter = new MainAdapter(myDataset, this);
mRecyclerView.setAdapter(mAdapter);
}
mRecyclerView.addItemDecoration(
new DividerItemDecoration(rootView.getContext(), DividerItemDecoration.VERTICAL));
mLayoutManager = new LinearLayoutManager(rootView.getContext());
mRecyclerView.setLayoutManager(mLayoutManager);
return rootView;
}
和PagerAdapter,只是默认实现:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
}
return null;
}
}
这些是我用来创建屏幕的所有相关方法。
答案 0 :(得分:1)
正在发生的事情是android正在破坏你的片段的视图,当你回来时,android正在重建那些视图而不破坏片段本身。
可能(没有代码很难猜测),您正在polyNewton :: (Fractional a, Ord a) => Poly a -> a -> a`
polyNewton p s = if (abs(polyValue p s) <= (0 + 1e-10)) then s else polyNewton
p (s - (polyValue p s) / (polyValue (polyDeriv p) s))
而不是onCreate
答案 1 :(得分:0)
过了一会儿,我发现了这个问题。在应用程序中有一个片段,根据其位置呈现不同的数据。我使用sectionId
来设置每个ViewPager页面上显示的内容:
if (sectionId == 1) {
mAdapter = new MainList1Adapter(list1, this);
mRecyclerView.setAdapter(mAdapter);
} else if (sectionId == 2) {
myDataset = meusItems.getFirstPart();
mAdapter = new MainAdapter(myDataset, this);
mRecyclerView.setAdapter(mAdapter);
} else if (sectionId == 3) {
myDataset = meusItems.getSecondPart();
mAdapter = new MainAdapter(myDataset, this);
mRecyclerView.setAdapter(mAdapter);
}
当应用程序在后台停留太久然后重新打开 sectionId
等于0 时,发生了什么。为解决此问题,我使用sectionId
保存了onSaveInstanceState
的值:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("sectionId", sectionId);
}
并在onCreateView
中检索此值:
if(savedInstanceState != null){
sectionId = savedInstanceState.getInt("sectionId");
}
答案 2 :(得分:0)
考虑使用 FragmentStatePagerAdapter 。如果有大量子片段,则是更好的选择。其次,您可能在片段中加载了太多视图。
替换:-
public class YourAdapterName extends FragmentPagerAdapter
与此:-
public class YourAdapterName extends FragmentStatePagerAdapter