我用recyclerView的时候设置LayoutManager为Grid,添加decoration为Grid,作为二级列表时,多次点击一级列表来跳转的时候,两张图之间的间隙在逐渐变大,后来发现是因为多次初始化Adaper的缘故。 
Android RecyclerView使用GridLayoutManager导致间隙变大的问题

mWaresAdapter = new WaresAdapter(getContext(), datas);
mRecyclerViewWares.setAdapter(mWaresAdapter);
mRecyclerViewWares.setLayoutManager(new GridLayoutManager(getContext(), 2));
mRecyclerViewWares.addItemDecoration(new DividerGridItemDecoration(getContext()));

这是有bug的代码,每次都创建adapter导致间隙变大

解决办法

if(mWaresAdapter == null) {
  mWaresAdapter = new WaresAdapter(getContext(), datas);  
  mRecyclerViewWares.setAdapter(mWaresAdapter)
  mRecyclerViewWares.setLayoutManager(new GridLayoutManager(getContext(), 2)); 
  mRecyclerViewWares.addItemDecoration(new DividerGridItemDecoration(getContext()));
 }else{
   mWaresAdapter.clearData();
   mWaresAdapter.addData(datas);
 }

 

相关文章:

  • 2021-05-17
  • 2022-12-23
  • 2021-04-02
  • 2022-01-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-25
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-24
  • 2022-12-23
  • 2021-10-26
  • 2022-12-23
相关资源
相似解决方案