1. 程式人生 > >recyclerview 新增分割線

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資源.