Android-长时间在后台重新打开应用程序时,Recyclerview空白为

时间:2019-09-16 18:18:26

标签: android android-recyclerview staggeredgridlayoutmanager

我具有以下应用程序结构:

- 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的适配器。

已经尝试了以下方法,但是没有运气:


编辑1:在片段FragmentStatePagerAdapter中,如果我将A替换为StaggeredGridLayoutManager,它将显示数据。所以问题似乎出在GridLayoutManager


编辑2:附加代码(简化的类/变量名,易于理解

  • TabAdapter -用于viewpager的适配器
StaggeredGridLayoutManager
  • 活动-包含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();
    }
}
  • 片段A -具有交错的网格布局
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
} 
  • 片段B -具有网格布局
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
}

0 个答案:

没有答案