RecyclerView沒有能夠直接設置間距的屬性,但能夠用ItemDecoration來裝飾一個item,因此繼承重寫ItemDecoration就能夠實現間距了 java
public class SpaceItemDecoration extends RecyclerView.ItemDecoration{ private int space; public SpaceItemDecoration(int space) { this.space = space; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { if(parent.getChildPosition(view) != 0) outRect.top = space; } }
int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.space); mRecyclerView.addItemDecoration(new SpaceItemDecoration(spacingInPixels));Adapter填充上,再看看item就有間距了