我用recyclerView的时候设置LayoutManager为Grid,添加decoration为Grid,作为二级列表时,多次点击一级列表来跳转的时候,两张图之间的间隙在逐渐变大,后来发现是因为多次初始化Adaper的缘故。
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); }