原来项目简单,一直使用的ListView,但是确实感觉不是很给力呀。算是改邪归正了,回到了大家都用的RecyclerView的怀抱了。非常舒服,但是也踩了很多坑,当然,现在的项目依然简单,肯定没踩完RecyclerView的坑,再接再厉,加油吧,诸君!
完整代码大家自己网上找找吧。
一、分割线问题
遇到几个坑,非常灵性,非常难受。
1)分割线显示问题
看了不少文章,非常多的都是自定义分割线,直接copy只能说看运气了,直接看下图吧
public class RecycleViewDivider extends RecyclerView.ItemDecoration{
// onDraw 方法是将 divider(分割线)画到item项下方,会被item项背景覆盖,网上都是这个方法,蛋疼
// @Override
// public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
// super.onDraw(c,parent,state);
// if (mOrientation == LinearLayoutManager.VERTICAL) {
// drawVertical(c, parent);
// } else {
// drawHorizontal(c, parent);
// }
// }
/**
* onDrawOver 方法是将 divider(分割线)画到item项上方,不会被item项背景覆盖
*/
@Override
public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
super.onDrawOver(c, parent, state);
if (mOrientation == LinearLayoutManager.VERTICAL) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
}
2)最后一个分割线隐藏问题
依然是网上copy,然后感觉就很奇怪,怎么底部会有一个分割线高度的空白,看代码吧
/**
* 绘制纵向 item 分割线
*
* @param canvas 画笔
* @param parent RecyclerView
*/
private void drawVertical(Canvas canvas, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getMeasuredWidth() - parent.getPaddingRight();
final int childSize = parent.getChildCount();
for (int i = 0; i < childSize - 1; i++) { //直接少画一条分割线(这里个人理解是上色,并不是占位)
final View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
final int top = child.getBottom() + layoutParams.bottomMargin;
int bottom = top + size;
// if (i != (childSize - 1)) { //不绘制最后一个分割线,for循环里修改了
canvas.drawRect(left, top, right, bottom, mPaint);
// }
}
}
/**
* 绘制横向 item 分割线
*
* @param canvas 画笔
* @param parent RecyclerView
*/
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();
final int childSize = parent.getChildCount();
for (int i = 0; i < childSize - 1; i++) {//直接少画一条分割线(这里个人理解是上色,并不是占位)
final View child = parent.getChildAt(i);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
final int left = child.getRight();
final int right = left + size + layoutParams.rightMargin;
canvas.drawRect(left, top, right, bottom, mPaint);
}
}
/**
* 设置item分割线的size
*
* @param outRect 边
* @param view item项的根布局
* @param parent RecyclerView
* @param state 自己看源码吧,看英文是RecyclerView的状态
*/
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildLayoutPosition(view);
int count = state.getItemCount();
if (mOrientation == LinearLayoutManager.VERTICAL) {
if (position == count - 1) { //分割线的占位(上面两个个人理解是上色),最后一条分割线不要留位置,不然最后一条数据后面有空白
outRect.set(0, 0, 0, 0);
} else {
outRect.set(0, 0, 0, size);
}
} else { //项目目前都是竖向列表,下面的代码没用过,注释了,有问题也不怪我
//如果是Grid不需要绘制每行最后一个的分割线
// if (parent.getLayoutManager() != null && parent.getLayoutManager() instanceof GridLayoutManager) {
// GridLayoutManager gridLayoutManager = (GridLayoutManager) parent.getLayoutManager();
// int spanCount = gridLayoutManager.getSpanCount();
// if ((position + 1) % spanCount == 0) {
// return;
// }
// }
// outRect.set(0, 0, size, 0);
}
}
3)RecyclerView设置显示隐藏的时候分割线越来越大的灵异事件
这个是最无语的,最开始都没管,谁会无聊的无限隐藏或显示呢。但是,我自己就这么做了,看到item项之间不断增加的间隔,异常蛋疼。好吧,网上找了找,看到有人说要判断RecyclerView是否已经添加了分割线,又就不添加了,确实是这个问题,很简单,但是很难受,直观的感觉是设置分割线应该是update,但是现实就是add,可恶!
//垂直布局
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
holder.recycler_view.setLayoutManager(linearLayoutManager);
//仅当recycler_view分割线数量为0的时候,才添加分割线
if (holder.recycler_view.getItemDecorationCount() == 0) {
//注意,需要放在设置LayoutManager之后,因为类里面自动判断了Layout类型
//添加横向分割线
//baseView.yyddRecyclerView.addItemDecoration(new RecycleViewDivider(LinearLayoutManager.HORIZONTAL, mContext.getBaseColor(R.color.color_ff3399), 10));
//添加纵向分割线
holder.adapter_yydd_order_list_recycler_view.addItemDecoration(new RecycleViewDivider(LinearLayoutManager.VERTICAL, mContext.getBaseColor(R.color.color_e5e5e5), MyApplication.dip2px(mContext, 15)));
}