参考http://www.jianshu.com/p/4eff036360da 文章可以为RecyclerView设置divider,但是碰到的几个问题让我对recyclerView很失望:
1.自定义的shape(GradientDrawable)无法显示,如果使用图片(BitmapDrawable)是可以正常显示的,另外在ItemDecoration中使用canva.drawRect可以绘制shape中的solid color。
2.ItemDecoration是以padding的方式存在,导致设置item的最小宽高必须算上divider,而且在item的RelativeLayout布局中设置margin不起作用,必须设置padding才有效果。
我搜索了很多文章感觉都是一个模子的,是在很困惑难道大家设置divider都没有一点问题,求大神解救。
你好,使用ItemDecoration设置分割线,布局中的margin是无效的,需要代码设置
public void drawHorizontal(Canvas c, RecyclerView parent)
{
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++)
{
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getLeft() - params.leftMargin;
final int right = child.getRight() + params.rightMargin
+ mDivider.getIntrinsicWidth();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}