1. 程式人生 > >RecyclerView 間距

RecyclerView 間距

RecyclerViews support the concept of ItemDecoration: special offsets and drawing around each element. As seen in this answer, you can use

publicclassSpacesItemDecorationextendsRecyclerView.ItemDecoration{privateint space;publicSpacesItemDecoration(int space){this.space = space;}@Overridepublicvoid
getItemOffsets(Rect outRect,View view,RecyclerView parent,RecyclerView.State state){ outRect.left = space; outRect.right = space; outRect.bottom = space;// Add top margin only for the first item to avoid double space between itemsif(parent.getChildPosition(view)==0) outRect.top = space
;}}
然後
mRecyclerView =(RecyclerView)findViewById(R.id.my_recycler_view);int spacingInPixels = 8;
mRecyclerView.addItemDecoration(newSpacesItemDecoration(spacingInPixels));