我有以下应用程序结构:
- Activity (ViewPager tabs)
- Fragment A -> Recyclerview with StaggeredGridLayoutManager
- Fragment B -> Recyclerview with GridLayoutManager
问题:在后台保持几个小时后打开应用程序后,recyclerviewFragment A
保持空白,但 recyclerviewFragment B
显示数据。两个recyclerviews 在它们的适配器中使用相同的arraylist 实例。
应用程序在短时间内最小化/恢复时不会出现问题。
我可以通过在开发人员选项中设置Background process limit
来重现这一点。No background processes
所以我的猜测是每次操作系统停止/终止进程时都会发生这种情况。
我正在使用一个扩展FragmentStatePagerAdapter
为我的 ViewPager 适配器的类。
已经尝试过以下方法,但没有运气:
https://stackoverflow.com/a/36099641(不能从 AppCompatActivity 调用 getChildFragmentManager)
- https://stackoverflow.com/a/28262885/6671004
编辑1:在片段中A
,如果我替换StaggeredGridLayoutManager
为GridLayoutManager
,它会显示数据。所以问题似乎出在StaggeredGridLayoutManager
编辑 2:附加代码(简化的类/变量名称以便于理解)
- TabAdapter - viewpager 的适配器
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();
}
}
- Activity - 包含 viewpager
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
}
- 片段 A - 具有交错网格布局
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
}
- 片段 B - 带有网格布局
public class FragmentB extends Fragment {
private RecyclerView mRecyclerView;
private FragmentBAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_b, container, false);
mRecyclerView = view.findViewById(R.id.recycler_view_b);
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
adapter = new FragmentBAdapter(getActivity(), MyActivity.myList);
mRecyclerView.setAdapter(adapter);
return view;
}
public void updateRecyclerViewData() {
if (adapter != null)
adapter.notifyDataSetChanged();
}
... other methods
}