/**
* 设置RecyclerView的间距
* 这样可以实现Grid和RecyclerView单条显示时,很好的区分开
*/
recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams)view.getLayoutParams();
int spanSize = layoutParams.getSpanSize();
int spanIndex = layoutParams.getSpanIndex();
// outRect.top = 20;
/**
* 不相等时说明是Grid形式显示的
* 然后判断是左边还有右边显示,分别设置间距为15
*/
if (spanSize!=manager.getSpanCount()){
switch (spanIndex){
case 0:
outRect.left = convertDpToPixel(15);
break;
case 1:
outRect.left = convertDpToPixel(7);
outRect.right = convertDpToPixel(7);
break;
case 2:
outRect.right = convertDpToPixel(15);
break;
}
}
}
});