recyclerview 添加分割线

使用recyclerview默认是没有没分割线的.在使用过程中需要添加分割线.先来看一下有没有分割线的区别

这是默认没有分割线的图片

这里是添加分割线的图

最简单的添加分割线的方法:

mRecycleView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));

只需要添加这句话,就出现有分割线的效果.
查看 添加分割线的源码

这里写图片描述

这个类可以用来个recyclerview添加分割线,只支持水平和垂直方向列表,并给出了示例代码.

查看源码发现有setdrawable方法

   /**
     * Sets the {@link Drawable} for this divider.
     *
     * @param drawable Drawable that should be used as a divider.
     */
    public void setDrawable(@NonNull Drawable drawable) {
        if (drawable == null) {
            throw new IllegalArgumentException("Drawable cannot be null.");
        }
        mDivider = drawable;
    }

通过该方法可以设置自定义的分割线.指定分割线的drawable资源.

猜你喜欢

转载自blog.csdn.net/f820306455/article/details/80581724