RecycleView之GridLayoutItemDecora
2020-12-17 本文已影响0人
wangxiaojin
1.自定义分割线
public class GridLayoutItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDriver;
// 网上绝大部分用的系统的一个属性 叫做 android.R.attrs.listDriver ,这个也可以,需要在清单文件中配置
public GridLayoutItemDecoration(Context context, int drawableRescourseId) {
// 解析获取 Drawable
mDriver = ContextCompat.getDrawable(context, drawableRescourseId);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
// 留出分割线的位置 每个item控件的下边和右边
outRect.bottom = mDriver.getIntrinsicHeight();
GridLayoutManager mgr;
if (parent.getLayoutManager() instanceof GridLayoutManager) {
mgr = (GridLayoutManager) parent.getLayoutManager();
} else {
return;
}
int position = parent.getChildAdapterPosition(view);
if (position == RecyclerView.NO_POSITION) {
// here I need to access the position of the current element
// and call outRect.set(left, top , right, bottom);
// which is not possible because it is no longer in the adapter
return;
}
int spanCount = mgr.getSpanCount();
int spanSize = mgr.getSpanSizeLookup().getSpanSize(position);
int spanIndex = mgr.getSpanSizeLookup().getSpanIndex(position, spanCount);
if (spanIndex == spanCount - 1) {
// last element
outRect.left = mDriver.getIntrinsicWidth() / 2;
outRect.right = mDriver.getIntrinsicWidth();
} else if (spanIndex == 0) {
// first element
outRect.left = mDriver.getIntrinsicWidth();
outRect.right = mDriver.getIntrinsicWidth() / 2;
} else {
// middle element
outRect.left = mDriver.getIntrinsicWidth()/2;
outRect.right = mDriver.getIntrinsicWidth() / 2;
}
}
// 绘制分割线
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
// 绘制分割线
// 绘制水平方向
drawHorizontal(c, parent);
// 绘制垂直方向
drawVertical(c, parent);
}
/**
* 绘制垂直方向的分割线
*
* @param c
* @param parent
*/
private void drawVertical(Canvas c, RecyclerView parent) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) childView.getLayoutParams();
int top = childView.getTop() - params.topMargin;
int bottom = childView.getBottom() + params.bottomMargin;
int left = childView.getRight() + params.rightMargin;
int right = left + mDriver.getIntrinsicWidth();
//计算水平分割线的位置
mDriver.setBounds(left, top, right, bottom);
mDriver.draw(c);
}
}
/**
* 绘制水平方向的分割线
*
* @param c
* @param parent
*/
private void drawHorizontal(Canvas c, RecyclerView parent) {
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View childView = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) childView.getLayoutParams();
int left = childView.getLeft() - params.leftMargin;
int right = childView.getRight() + mDriver.getIntrinsicWidth() + params.rightMargin;
int top = childView.getBottom() + params.bottomMargin;
int bottom = top + mDriver.getIntrinsicHeight();
//计算水平分割线的位置
mDriver.setBounds(left, top, right, bottom);
mDriver.draw(c);
}
}
}
创建分割线颜色和大小drawable\item_divider_white.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/white" />
<size
android:width="15dp"
android:height="15dp" />
</shape>