复制@edwardaa提供的代码和和我完美地支持实时通讯RTL(实时限值):
公共类GridSpacingItemDecoration扩展了RecyclerView。项目装饰{ 私有int spanCount; 私有int间距; private boolean includeEdge; private int headerNum; 私有布尔值isRtl=TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault())==ViewCompat。布局_方向_ RTL; public GridSpacingItemDecoration(int spanCount,int spacing,boolean includeEdge,int headerNum){ this.spanCount=span计数; this.space=间距; this.includeEdge=包含边缘; this.headerNum=头编号; } @覆盖 public void getItemOffsets(Rect outRect,View视图,RecyclerView父级,RecyclerView.State状态){ int position=parent.getChildAdapterPosition(视图)-headerNum;//项目位置 if(位置>=0){ int column=位置%spanCount;//项目列 如果(isRtl){ column=spanCount-1-列; } if(包括Edge){ outRect.left=间距-列*间距/spanCount;//间距-列*((1f/spanCount)*间距) outRect.right=(column+1)*间距/spanCount;//(列+1)*((1f/spanCount)*间距) if(位置<spanCount){//上边缘 outRect.top=间距; } outRect.bottom=间距;//项目底部 }其他{ outRect.left=列*间距/spanCount;//列*((1f/spanCount)*间距) outRect.right=间距-(列+1)*间距/spanCount;//间距-(列+1)*((1f/spanCount)*间距) if(位置>=spanCount){ outRect.top=间距;//项目顶部 } } }其他{ outRect.left=0; outRect.right=0; outRect.top=0; outRect.bottom=0; }}}
}