我具有以下应用程序结构:
- Activity (ViewPager tabs)
- Fragment A -> Recyclerview with StaggeredGridLayoutManager
- Fragment B -> Recyclerview with GridLayoutManager
问题:在将应用在后台放置几个小时后打开应用后,Fragment A
中的recyclerview保持空白,但{{ 1}} 显示数据。两种recyclerview都在其适配器中使用相同的arraylist实例。
当在短时间内最小化/恢复时,不会发生问题。
我可以通过在开发人员选项中将Fragment B
设置为Background process limit
来重现此内容。因此,我的猜测是每次操作系统停止/杀死进程时都会发生这种情况。
我正在使用扩展No background processes
的类作为ViewPager的适配器。
已经尝试了以下方法,但是没有运气:
https://stackoverflow.com/a/36099641(无法从AppCompatActivity调用getChildFragmentManager)
编辑1:在片段FragmentStatePagerAdapter
中,如果我将A
替换为StaggeredGridLayoutManager
,它将显示数据。所以问题似乎出在GridLayoutManager
编辑2:附加代码(简化的类/变量名,易于理解)
StaggeredGridLayoutManager
public class TabAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public TabAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
}
public class MyActivity extends AppCompatActivity implements TabLayout.BaseOnTabSelectedListener {
public static List<MyPojo> mylist = new ArrayList<>();
private TabAdapter adapter;
private TabLayout tabLayout;
private ViewPager viewPager;
private FragmentA fragmentA;
private FragmentB fragmentB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_landing);
viewPager = findViewById(R.id.viewPager);
tabLayout = findViewById(R.id.tabLayout);
adapter = new TabAdapter(getSupportFragmentManager());
mylist.add(...) // arraylist populated
fragmentA = new FragmentA();
fragmentB = new FragmentB();
adapter.addFragment(fragmentA, "A");
adapter.addFragment(fragmentB, "B");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.addOnTabSelectedListener(this);
}
... other methods
}
public class FragmentA extends Fragment {
private RecyclerView mRecyclerView;
private FragmentAAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a, container, false);
mRecyclerView = view.findViewById(R.id.recycler_view_a);
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, 1));
adapter = new FragmentAAdapter(getActivity(), MyActivity.myList);
mRecyclerView.setAdapter(adapter);
return view;
}
public void updateRecyclerViewData(){
if (adapter != null)
adapter.notifyDataSetChanged();
}
... other methods
}