原文:Android——RecyclerView入門學(xué)習(xí)之ItemDecoration
簡單效果:水平方向第一個(gè)和最后一個(gè)加間距
項(xiàng)目里有一個(gè)水平滑動(dòng)的RecyclerView,第一個(gè)和最后一個(gè)距離手機(jī)兩邊間距大一點(diǎn),其他的間距都靠加的背景圖帶了點(diǎn)距離撐開
以前的我都是在item里左右都放一個(gè)View,判斷第0個(gè)的時(shí)候顯示左邊的View,最后一個(gè)顯示右邊的View,其他的兩個(gè)View都隱藏,方法很笨,但是也實(shí)現(xiàn)了
但是強(qiáng)大的RecyclerView會(huì)教你優(yōu)雅的實(shí)現(xiàn)間距,就是 ItemDecoration
看了文章寫了一個(gè)可以實(shí)現(xiàn)上述效果的一個(gè)簡單ItemDecoration,這里記錄一下
/**
*水平滑動(dòng)的RecyclerView:第0個(gè)和第size-1個(gè)加間距尔许,其他的不加
*垂直滑動(dòng)的RecyclerView:每個(gè)底部加36的間距
*/
public class RecyclerViewLeftRightAddPadding extends RecyclerView.ItemDecoration {
private int drawPadding = 36;
private int orientation;
private Paint paint;
public RecyclerViewLeftRightAddPadding(Context context, int orientation) {
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(context.getResources().getColor(R.color.white));
this.orientation = orientation;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
//加一個(gè)偏移量
RecyclerView.LayoutManager manager = parent.getLayoutManager();
int index = parent.getChildAdapterPosition(view);
int listSize = state.getItemCount();
if (manager instanceof LinearLayoutManager) {
if (orientation == LinearLayoutManager.VERTICAL) {
//垂直
outRect.set(0, 0, 0, drawPadding);
} else {
//水平
if (index == 0) {
outRect.set(drawPadding, 0, 0, 0);
} else if (index == listSize - 1) {
outRect.set(0, 0, drawPadding, 0);
} else {
outRect.set(0, 0, 0, 0);
}
}
}
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
int listSize = state.getItemCount();
// LogUtil.d("listSize = " + listSize);
int childCount = parent.getChildCount();
// LogUtil.d("childCount = " + childCount);
for (int i = 0; i < childCount; i++) {
View view = parent.getChildAt(i);
int index = parent.getChildAdapterPosition(view);
// LogUtil.d("index = " + index);
if (orientation == LinearLayoutManager.VERTICAL) {
//垂直
float top = view.getBottom();
float bottom = view.getBottom() + drawPadding;
float left = view.getLeft();
float right = view.getRight();
c.drawRect(left, top, right, bottom, paint);
} else {
//水平
if (index == 0) {
c.drawRect(view.getLeft() - drawPadding, view.getTop(), view.getLeft(), view.getBottom(), paint);
// LogUtil.d("left = " + (view.getLeft() - drawPadding) + ",top = " + top + ",right = " + view.getRight() + ",bottom = " + bottom);
} else if (index == listSize - 1) {
c.drawRect(view.getRight(), view.getTop(), view.getRight() + drawPadding, view.getBottom(), paint);
// LogUtil.d("left = " + view.getLeft() + ",top = " + top + ",right = " + (view.getRight() + drawPadding) + ",bottom = " + bottom);
} else {
c.drawRect(view.getLeft(), view.getTop(), view.getLeft(), view.getBottom(), paint);
// LogUtil.d("left = " + view.getLeft() + ",top = " + top + ",right = " + view.getRight() + ",bottom = " + bottom);
}
}
}
}
調(diào)用
manager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.addItemDecoration(new RecyclerViewLeftRightAddPadding(this, LinearLayoutManager.HORIZONTAL));
horizontalAdapter = new RecyclerViewHorizontalAdapter(this, list);
recyclerView.setAdapter(horizontalAdapter);