我在ViewPager
中有4个片段。其中有3个具有静态数据,另一个是从远程服务器加载数据。当我运行程序时,所有带有静态RecyclerView
数据的片段都加载得很好。但是只有在我离开页面然后重新选择选项卡(或向后导航)后,才会显示具有动态数据的页面。
我已经尝试过Update ViewPager dynamically?中介绍的解决方案,但无法正常工作
private void initialize(){
//Create fragments and setup viewpager
frag_categories = new Fragment_Categories();
frag_deals = new Fragment_Deals();
frag_favourites = new Fragment_Favorites();
frag_sangwapo = new Fragment_Sangwapo();
setupViewPager(viewPager);
//prepare tabLayout
tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
tabLayout.getTabAt(1).select();
tabLayout.addOnTabSelectedListener(this);
}
private void setupViewPager(ViewPager viewPager) {
adapter = new AdapterViewPager(getSupportFragmentManager());
adapter.addFragment(frag_favourites, "Stores");
adapter.addFragment(frag_categories, "Categories");
adapter.addFragment(frag_deals, "Deals");
adapter.addFragment(frag_sangwapo, "Sangwapo");
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(0);
}
//在片段内部没有刷新
adapterSangwapo = new AdapterSangwapo(getActivity(),list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(adapterSangwapo);
updateListItems();
其中updateListItems正在填充ArrayList(列表)并在adaptersangwapo上调用notifyDataSetChanged
即使我尝试使用viewPager.setOffscreenPageLimit(3);
,它仍然不会刷新,直到我离开。我也尝试过FragmentPagerAdapter
和FragmentStatePagerAdapte
没有显示错误。