rambo_huang 2017-01-25 06:55 采纳率: 0%
浏览 2154
已结题

RecyclerView设置divider的问题

参考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都没有一点问题,求大神解救。

  • 写回答

1条回答 默认 最新

  • 风晴03 2017-01-25 07:54
    关注

    你好,使用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);
            }
        }
    

    效果图
    图片说明

    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站