RecyclerView 間距
阿新 • • 發佈:2019-02-13
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));