我有一个使用gridlayout的recyclerview,如下所示。我想以这样一种方式设置项目装饰,即每行显示4个项目,并带有边框,并且边框之间没有空格。
recyclerView = findViewById(R.id.gridItems);
List<Amenity> amenities = createAmenities();
AmenitiesGridAdapter amenitiesGridAdapter = new AmenitiesGridAdapter(AmenitiesActivity.this, amenities);
recyclerView.setLayoutManager(new GridLayoutManager(AmenitiesActivity.this,4));
recyclerView.setAdapter(amenitiesGridAdapter);
我还有物品装饰,可以为recyclerview设置如下图所示
AmenitiesItemDecoration lineService = new AmenitiesItemDecoration(this, 4, amenities.size());
recyclerView.addItemDecoration(lineService);
下面是我的物品装饰课
public class AmenitiesItemDecoration extends RecyclerView.ItemDecoration{
private int[] attrs = new int[]{
android.R.attr.listDivider
};
private Drawable divider;
private int mColumn;
private Context mContext;
private int mTotalCount;
private boolean isAllScreen = true;
public AmenitiesItemDecoration(Context context, int column, int totalCount) {
super();
mContext = context;
TypedArray typedArray = context.obtainStyledAttributes(attrs);
divider = typedArray.getDrawable(0);
mTotalCount = totalCount;
mColumn = column;
}
public void isAllScreen(boolean isAllScreen) {
this.isAllScreen = isAllScreen;
}
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
drawLine(parent, c);
}
private void drawLine(RecyclerView parent, Canvas c) {
Paint paint = new Paint();
paint.setColor(mContext.getResources().getColor(R.color.colorAccent)); //#FF4081
paint.setStrokeWidth(Px2DpUtil.dp2px(mContext, 1.5f));
int childCount = parent.getChildCount();
mTotalCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = parent.getChildAt(i);
float x = childView.getX();
float y = childView.getY();
float width = childView.getWidth();
float height = childView.getHeight();
if (i % mColumn == 0) {
View view = parent.getChildAt(i);
float x1 = view.getX();
float y1 = view.getY();
//top
c.drawLine(x1, y1, ScreenUtil.getScreenWidth(mContext), y1, paint);
}
if ((i + 1) % mColumn != 0) {
//right
c.drawLine(x + width, y, x + width, y + height, paint);
}
int remainder = mTotalCount % mColumn;
int temp = mTotalCount - i;
if (isAllScreen) {
if ((temp <= remainder) || (temp <= mColumn && remainder == 0)) {
//bottom
c.drawLine(x, y + height, ScreenUtil.getScreenWidth(mContext), y + height, paint);
//right
c.drawLine(x + width, y, x + width, y + height + divider.getIntrinsicHeight() / 2, paint);
}
} else {
if ((temp <= remainder) || (temp <= mColumn && remainder == 0)) {
//bottom
c.drawLine(x, y + height, x + width, y + height, paint);
//right
c.drawLine(x + width, y, x + width, y + height + divider.getIntrinsicHeight() / 2, paint);
}
}
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State
state) {
int left = 0;
int top = 0;
int right = 0;
int bottom = 0;
int position = parent.getChildLayoutPosition(view);
int remainder = mTotalCount % mColumn;
int temp = mTotalCount - position;
if ((temp <= remainder) || (temp <= mColumn && remainder == 0)) {
bottom = divider.getIntrinsicHeight();
}
outRect.set(left, top, right, bottom);
}
}
我的挑战是物品装饰正在显示,我不确定我在哪里种植它。