設定RecyclerView專案之間的間距
阿新 • • 發佈:2019-01-10
Simple ItemDecoration implementation for equal spaces between all items.
public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
int mSpace ;
/**
* @param space 傳入的值,其單位視為dp
*/
public SpaceItemDecoration(int space) {
this.mSpace = UIUtil.dp2px(space);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int itemCount = mAdapter.getItemCount();
int pos = parent.getChildAdapterPosition(view);
Log.d(TAG, "itemCount>>" +itemCount + ";Position>>" + pos);
outRect.left = 0 ;
outRect.top = 0;
outRect.bottom = 0;
if (pos != (itemCount -1)) {
outRect.right = mSpace;
} else {
outRect.right = 0;
}
}
}
然後設定
recyclerView.addItemDecoration(new SpaceItemDecoration(5));